RandomAccessFile打印到文本文件有问题

时间:2012-08-06 19:14:10

标签: java random-access

嘿我正在使用来自ArrayList商店的Student数组使用文件i / o写入文本文件。这一切都很好。我有那个工作。但在实际的文本文件中,细节是一行打印和阅读。

任何人都可以看到问题。

这是我的代码:

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", "bexo@hotmail.com");
        Student b = new Student("Fabio Borini", "DKIT28", "0876136944", "fabioborini@gmail.com");
        Student c = new Student("Gaston Ramirez", "DKIT29", "0419834501", "gramirez@webmail.com");
        Student d = new Student("Luis Suarez", "DKIT7", "0868989878", "luissuarez@yahoo.com");
        Student e = new Student("Andy Carroll", "DKIT9", "0853456788", "carroll123@hotmail.com");
        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";
    }

输出 dent ---------------------------学生姓名:Becky O'Brien学生编号:DKIT26学生电话号码:0876126944

2 个答案:

答案 0 :(得分:2)

我认为这不是搞乱的写作;

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

当你调用readLine时,它会得到当前行,但是你必须自己添加换行符(\ n),所以它应该是file.readLine()+“\ n”而不是file.readLine( )

答案 1 :(得分:2)

如果您在某些文本编辑器中查看输出,则可能无法将“\ n”解析为行分隔符(例如,在Windows上的记事本中)。从这个question中选择解决方法。

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