Эй, я пишу в текстовый файл, используя файловый ввод-вывод, используя массив студентов из хранилища ArrayList. Это все хорошо и хорошо. у меня так работает. Но в самом текстовом файле детали печатаются и читаются в одной строке.
Может кто видит проблему.
Вот мой код:
Основное приложение
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.
//---------------------------------------------------------------------------
public String toString()
{
return "---------------------------Student--------------------------- " +
"\nStudent Name:" + studentName +
"\nStudent Id:"+ studentId +
"\nStudent Telephone Number:"+ studentTelephoneNumber +
"\nStudent Email:" + studentEmail +"\n\n";
}
Выходная вмятина -------------------------- Имя студента: Бекки О'БрайенИдентификатор студента: DKIT26Номер телефона студента: 0876126944