Java 使用RandomAccessFile编写学生数组时遇到问题

Java 使用RandomAccessFile编写学生数组时遇到问题,java,arrays,arraylist,random-access,Java,Arrays,Arraylist,Random Access,我在打印一个学生商店时遇到了一些问题,我使用了ArrayList。然后我制作了一个静态数组来容纳这些不同的学生,现在我试图找出我可以使用什么方法来编写它们。这是我的密码: MainApp import java.io.RandomAccessFile; public class MainApp { public static void main(String[] args) throws Exception { n

我在打印一个学生商店时遇到了一些问题,我使用了
ArrayList
。然后我制作了一个静态数组来容纳这些不同的学生,现在我试图找出我可以使用什么方法来编写它们。这是我的密码:

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");

            Student[] students = {a,b,c,d,e};
            for (int i = 0;i < students.length;i++)
            {
                file.writeByte(students[i]);
            }
            file.close();


         }


     }
MainApp
导入java.io.RandomAccessFile;
公共类MainApp
{
公共静态void main(字符串[]args)引发异常
{
新建MainApp().start();
}
public void start()引发异常
{
StudentStore details=新建StudentStore();
学生a=新学生(“贝基·奥布莱恩”、“DKIT26”、“0876126944”bexo@hotmail.com");
学生b=新学生(“法比奥·博里尼”、“DKIT28”、“0876136944”fabioborini@gmail.com");
学生c=新学生(“加斯顿·拉米雷斯”、“DKIT29”、“0419834501”gramirez@webmail.com");
学生d=新学生(“路易斯·苏亚雷斯”、“DKIT7”、“08689878”luissuarez@yahoo.com");
学生e=新学生(“安迪·卡罗尔”、“DKIT9”、“0853456788”carroll123@hotmail.com");
详情.加入(a);
详情.加入(b);
详情.加入(c);
详情.添加(d);
详情.添加(e);
//details.print();
RandomAccessFile文件=新的RandomAccessFile(“ContactDetails.txt”、“rw”);
学生[]学生={a,b,c,d,e};
for(int i=0;i

file.writeByte(学生[i])
不正确,我找不到适合的方法。错误读取类型
RandomAccessFile
中的方法
writeByte(int)
不适用于参数(
Student
)。这显然是因为
writeBytes
方法不采用student类型。

字符串有一种非常简单的转换为字节的方法。他们有一个
getBytes()
方法,可以很好地工作。通过重载
toString()
方法,可以获得学生的字符串表示形式。所以你的电话看起来很像

file.writeByte(students[i].toString().getBytes( "UTF-8" ));
编辑:

忘记getBytes()返回字节数组。这应该起作用:

byte[] bytes = students[i].toString().getBytes();
for(byte byteWrite : bytes){
    file.writeByte(byteWrite);
}

RandomAccessFile
旨在查找文件中的点并注入值,我不建议您使用它。如果您只想将这些行写入文件,我会使用
BufferedWriter
。它有你需要的一切

BufferedWriter文件=新的BufferedWriter(新的FileWriter(文件名))

然后写,只要写:

(学生[i])

我应该警告你,除非你有一个自定义的
toString()
方法,否则每个学生看起来都像垃圾


如果您只想以不可读的格式将对象写入文件,请检查
writeByte
是否需要一个int,您传入了一个完整的
Student
object.Correct。我知道。我想知道的是可以使用什么方法。你到底想写什么?听起来你需要一个
BufferedWriter
,只需实现你自己的
ToString()
方法来定制学生打印出来的样子。我必须使用RandomAccessFile:(你的教授只允许你使用RandomAccessFile?这似乎有点愚蠢。教你只使用一种方法做某事似乎是教软件开发的一种倒退的方法。我们已经做了正常的文件i/o。我在这方面做得很好,但在昨天之前我从未听说过RandomAccessFile。英国皇家空军有一些很好的用途,但是这不是他们的强项。不过我很高兴你的问题解决了!祝你的疯狂老师好运!你是否推翻了toString()?你如何推翻它?我知道没有问题。但我以前从未这样做过。非常简单。只需声明一个名为toString()的方法在你的学生课上。请看@Pendo826。我也更新了我的答案。犯了一个小错误。你从哪里得到第一行的学生?