2012-08-06 3 views
1

ArrayList 저장소의 학생 배열을 사용하여 파일 I/O를 사용하여 텍스트 파일에 쓰고 있습니다. 그게 다 좋고 좋아. 나는 일하고있다. 그러나 실제 텍스트 파일에서 세부 정보는 한 줄로 인쇄하고 읽는 것입니다.RandomAccessFile 텍스트 파일로 인쇄 문제

누구든지이 문제를 볼 수 있습니까? -----------------

MainApp

import java.io.RandomAccessFile; 



public class MainApp 
{ 

    public static void main(String[] args) throws Exception 
    { 
     new MainApp().start(); 

    } 
    public void start()throws Exception 
    { 
     StudentStore details = new StudentStore(); 
     Student a = new Student("Becky O'Brien", "DKIT26", "0876126944", "[email protected]"); 
     Student b = new Student("Fabio Borini", "DKIT28", "0876136944", "[email protected]"); 
     Student c = new Student("Gaston Ramirez", "DKIT29", "0419834501", "[email protected]"); 
     Student d = new Student("Luis Suarez", "DKIT7", "0868989878", "[email protected]"); 
     Student e = new Student("Andy Carroll", "DKIT9", "0853456788", "[email protected]"); 
     details.add(a); 
     details.add(b); 
     details.add(c); 
     details.add(d); 
     details.add(e); 
     //details.print(); 


     RandomAccessFile file = new RandomAccessFile("ContactDetails.txt","rw"); 
     //getBytes() returns an array of bytes. 
     //Because i have put the store in a static Array.(I done this because i could find no other 
     //Simple way to write a Student Object.) 
     //None of the methods of the RandomAccessFile write class worked with this. 
     Student[] students = {a,b,c,d,e}; 
     for (int i = 0; i < students.length; i++) 
     { 
     byte[] bytes = students[i].toString().getBytes(); 
     for(byte byteWrite : bytes) 
     { 
      file.writeByte(byteWrite); 
     } 
     } 
     final int Record_Length = 30; 
     int recordNumber = 1; 
     file.seek((recordNumber) * Record_Length); 

     String code =""; 
     for(int i = 0; i < 4; i++) 
     { 
     code += file.readLine(); 
     } 
     System.out.println(code); 
     file.close(); 


    } 


} 

ToString

//--------------------------------------------------------------------------- 
// toString. 
//--------------------------------------------------------------------------- 
    public String toString() 
    { 
     return "---------------------------Student--------------------------- " + 
       "\nStudent Name:" + studentName + 
       "\nStudent Id:"+ studentId + 
       "\nStudent Telephone Number:"+ studentTelephoneNumber + 
       "\nStudent Email:" + studentEmail +"\n\n"; 
    } 

출력 덴트 :

여기

내 코드입니다 ---------- 학생 이름 : Becky O'Brien 학생 : DKIT26 학생 번호 : 0876126944

답변

2

wr 내가 생각하기에 엉망이되고있는 iting;

String code =""; 
    for(int i = 0; i < 4; i++) 
    { 
    code += file.readLine(); 
    } 
    System.out.println(code); 

당신 내의 readLine를 호출 할 때, 그것은 현재 행을 얻을 수 있지만, 당신은 자신의 줄 바꿈 (\ n)를 추가, 그래서 그냥 대신 + "\ n"file.readLine()해야한다 file.readLine()

+0

감사합니다. 또한 IDE에서 사용하는 텍스트 파일을로드 할 때 모든 것이 순서대로 이루어집니다. 하지만 프로젝트 폴더에서 한 줄에 모두 열 때. 어떤 생각이야? – Pendo826

+0

그래서 IDE의 패키지 탐색기에서 파일을 열면 정상적으로 나타납니다.하지만 시스템 파일 탐색기로 가서 열면 줄 바꿈이 없습니다? –

+0

그 모든 것을 하나의 단일 라인에 입력하십시오. 솔로 션을 사용하면 100 % 올바른 형식으로 읽음을 표시했기 때문에 의미가 없습니다. S – Pendo826

2

일부 텍스트 편집기에서 출력을보고있는 경우 '\ n'을 줄 구분 기호 (예 : Windows의 메모장)로 구문 분석하지 못할 수도 있습니다. 이 question에서 해결할 방법을 선택하십시오.

그렇지 않으면 콘솔 출력 인 file.readLine()이 줄 바꿈에서 멈추지 만 문자열에 추가되지 않으므로 모든 내용이 한 줄에 인쇄됩니다. 각 행 다음에 직접 개행 문자를 추가해야합니다.