RandomAccessFile打印到文本文件的问题

问题描述:

嗨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

这不是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

如果您在某些文本编辑器中查看输出,可能不会将'\ n'解析为行分隔符(例如在Windows上的记事本中)。选择你的方式来解决这个question

否则,如果这是您的控制台输出,file.readLine()停止在换行符,但不会将它添加到字符串中,所以这就是为什么它全部打印在一行上。你必须在每行之后自行添加换行符。