gpt4 book ai didi

java - 是否可以使用 RandomAccessFile 打印出 Student 对象?

转载 作者:行者123 更新时间:2023-12-04 05:36:56 24 4
gpt4 key购买 nike

我正在使用 RandomAccessFile 为文本文件创建数据库。基本上我使用 ArrayList 创建了一个普通商店,现在我需要使用 RandomAccessFile 输出商店的内容。但我坚持如何获取 randomAccessFile 方法来获取学生商店。这是我的代码:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;


public class MainApp
{
private RandomAccessFile File;

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




//Create a file object.
File contactDetailsFile = new File("StudentDetails.txt");
//Open a buffered output stream to allow write to file operations.
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(contactDetailsFile)));

out.println(a);
out.println(b);
out.println(c);
out.println(d);
out.println(e);
out.close();

BufferedReader in = new BufferedReader(
new FileReader(contactDetailsFile));

String line = in.readLine();
System.out.println(line);

out.close();

}
/**
* @param args the command line arguments
*/
public void RandomAccessFile(String filename)
{

RandomAccessFile randomAccessFile = null;
try {

//Declare variables that we're going to write
String line1 = "First line\n";
String line2 = "Second line\n";

//Create RandomAccessFile instance with read / write permissions
randomAccessFile = new RandomAccessFile(filename, "rw");

//Write two lines to the file
randomAccessFile.writeBytes(line1);
randomAccessFile.writeBytes(line2);

//Place the file pointer at the end of the first line
randomAccessFile.seek(line1.length());

//Declare a buffer with the same length as the second line
byte[] buffer = new byte[line2.length()];

//Read data from the file
randomAccessFile.read(buffer);

//Print out the buffer contents
System.out.println(new String(buffer));

} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {

if (randomAccessFile != null)
randomAccessFile.close();

} catch (IOException ex) {
ex.printStackTrace();
}
}

}

}

最佳答案

一句话,是的。

但是,您将不得不再次重新执行此解决方案的写入文件部分。您必须以与写入对象相同的方式读取对象。

从这里 example ,您可以使用 RandomAccessFile 查看每个成员的编写方式对象。

我建议您让示例中的 read 方法返回一个新的 Student 对象。

关于java - 是否可以使用 RandomAccessFile 打印出 Student 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11812240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com