2012-08-06 59 views
1

嗨Im使用文件I/O使用ArrayList商店中的學生數組寫入文本文件。這一切都很好,很好。我有這個工作。但是在實際的文本文件中,細節在一行中打印和閱讀。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我覺得這是搞砸了。

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

當你打電話的readLine,它得到當前行,但你必須添加自己的換行符(\ n)的,所以它應該是file.readLine()+「\ n」,而不僅僅是file.readLine()

+0

謝謝你的工作。此外,當我加載即時通訊在IDE中使用的文本文件時,一切都是按順序進行的。但是,當我打開它在項目文件夾中的所有在一行上。有任何想法嗎? – Pendo826 2012-08-06 19:20:51

+0

因此,當您在IDE中的包資源管理器中打開該文件時,它顯示正常,但是如果您轉到系統文件資源管理器並打開它,它沒有換行符? – 2012-08-06 19:22:16

+0

不在一條線上。這是沒有意義的,因爲使用soloution時,您向我展示了其100%正確格式的讀取:S – Pendo826 2012-08-06 19:24:30

2

如果您在某些文本編輯器中查看輸出,可能不會將'\ n'解析爲行分隔符(例如在Windows上的記事本中)。選擇你的方式來解決這個question

否則,如果這是您的控制檯輸出,file.readLine()停止在換行符,但不會將它添加到字符串中,所以這就是爲什麼它全部打印在一行上。你必須在每行之後自行添加換行符。