Java “将对象写入文件”;个人.obj“;

Java “将对象写入文件”;个人.obj“;,java,file,bufferedreader,filereader,printwriter,Java,File,Bufferedreader,Filereader,Printwriter,嗨,各位飞越者。我有一个关于Java的问题,我应该把所有对象p1-p4放在一个名为“personen.obj”的文件中,对于p1,将所有数据写入“persoon1.obj”等 这是我正在使用的代码,它不包括计算机类,因为我认为这不是必需的 这包括一些荷兰语字符串,但每个人的数据都有一个toString。应该发生的是所有的ToString都写在这些文件中 import java.util.Calendar; public class Main { public static void main(

嗨,各位飞越者。我有一个关于Java的问题,我应该把所有对象p1-p4放在一个名为“personen.obj”的文件中,对于p1,将所有数据写入“persoon1.obj”等

这是我正在使用的代码,它不包括计算机类,因为我认为这不是必需的

这包括一些荷兰语字符串,但每个人的数据都有一个toString。应该发生的是所有的ToString都写在这些文件中

import java.util.Calendar;

public class Main {
public static void main(String[] args) {
    int huidigJaar = Calendar.getInstance().get(Calendar.YEAR);
    int aanschafJaarC1 = huidigJaar - 4; // c1 is 4 jaar oud
    int aanschafJaarC2 = huidigJaar - 3; // c2 is 3 jaar oud
    Persoon p1 = new Persoon("Eric", 20000);
    Persoon p2 = new Persoon("Hans", 60000);
    Persoon p3 = new Persoon("Willem-Alexander", 8500000);
    Computer c1 = new Computer("Medion", 2000, aanschafJaarC1, "Super");
    Computer c2 = new Computer("Dell", 1500, aanschafJaarC2, "Home");
    if (p1.koop(c1)) {
        System.out.println("Deze koper bezit nu nog " + p1.getBudget());
    }
    if (p1.koop(c1)) {
        System.out.println("Deze koper bezit nu nog " + p1.getBudget());
    }
    if (p2.koop(c1)) {
        System.out.println("Deze koper bezit nu nog " + p2.getBudget());
    }
    if (p2.koop(c2)) {
        System.out.println("Deze koper bezit nu nog " + p2.getBudget());
    }
    if (p3.koop(new Computer("Dell", 1500, aanschafJaarC2, "Home"))) {
        System.out.println("Deze koper bezit nu nog " + p3.getBudget());
    }
    if (p3.koop(c2)) {
        System.out.println("Deze koper bezit nu nog " + p3.getBudget());
    }
    System.out.println("\n" + p1);
    System.out.println(p2);
    System.out.println(p3);
    if (p1.verkoop(c1, p3)) {
        System.out.println("Deze verkoper bezit nu nog " + p1.getBudget());
    }
    if (p2.verkoop(c1, p3)) {
        System.out.println("Deze verkoper bezit nu nog " + p2.getBudget());
    }
    if (p2.verkoop(c2, p1)) {
        System.out.println("Deze verkoper bezit nu nog " + p2.getBudget());
    }
    System.out.println("\n" + p1);
    System.out.println(p2);
    System.out.println(p3);
}
}

对于简单地将少量字符串写入文件,我建议使用“PrintWriter”类。它围绕着一个标准的Writer进行封装,允许您将任何基本数据类型和一些对象写入给定的“Writer”(在本例中为FileWriter)

当然,这不是写入文件的唯一方法,也不是最好的方法

我已经很快从内存中编写了一些代码,我还没有测试过它,但我很确定它能工作

/**
 * This method will write an array of strings to the given file.
 * If the append flag is set to true, the new data will be added to the end of the file.
 * 
 * Note:
 *  This is not only nor the best way to write to a file.
 * 
 * @param filename - The path to the file you want to write to.
 * @param data - The Strings you want to write to the file.
 * @param append - Should the new data be added to the end of the file?
 * @return If the write operation succeeded.
 */
public static boolean writeStringsToFile(String filename, String[] data, boolean append) {
    boolean resultFlag = true;

    // The file class represents a file on a disk and provides some useful methods
    File file = new File(filename);
    //PrintWriter is used to write various data types to a given writer.
    PrintWriter printWriter = null;

    try {
        //This method will create an empty file, if there's not already one.
        //Will throw an IOException if it experiences an error creating the file.
        file.createNewFile();

        //A PrintWriter needs some kind of writer to output to, we'll use a FileWriter.
        //FileWriter is used for writing to files.
        //Will throw an IOException if the file doesn't exist (It should exist though)
        printWriter = new PrintWriter(new FileWriter(file, append));

        for(int i = 0; i < data.length; i++) {
            //Write the strings to the file, each on a new line.
            printWriter.println(data[i]);
        }

    } catch (IOException e) {
        //Uh oh. There was an error writing to the disk!
        e.printStackTrace();

        //We couldn't write to the disk, make sure we return false to let the caller know.
        resultFlag = false;
    } finally {
        //First check that we managed to create a PrintWriter before we try to close it.
        if (printWriter!=null)
            printWriter.close(); //Release the file from use.
    }

    return resultFlag;
}
/**
*此方法将向给定文件写入字符串数组。
*如果append标志设置为true,则新数据将添加到文件末尾。
* 
*注:
*这不仅不是最好的写入文件的方式。
* 
*@param filename-要写入的文件的路径。
*@param data-要写入文件的字符串。
*@param append-是否应将新数据添加到文件末尾?
*@如果写入操作成功,则返回。
*/
公共静态布尔writeStringsToFile(字符串文件名、字符串[]数据、布尔追加){
布尔结果滞后=真;
//file类表示磁盘上的文件,并提供一些有用的方法
文件=新文件(文件名);
//PrintWriter用于将各种数据类型写入给定的写入器。
PrintWriter PrintWriter=null;
试一试{
//如果还没有空文件,此方法将创建一个空文件。
//如果创建文件时遇到错误,将引发IOException。
createNewFile();
//PrintWriter需要某种类型的写入程序才能输出,我们将使用FileWriter。
//FileWriter用于写入文件。
//如果文件不存在,将抛出IOException(但应该存在)
printWriter=新的printWriter(新的FileWriter(文件,追加));
对于(int i=0;i
也值得注意编码。 某些系统可以/将以不同于其他系统的方式写入文本文件,在大多数小情况下,这不是问题,但可能是潜在的

我强烈建议您阅读一些oracle教程:


另外,请务必阅读其他一些网站,因为大多数网站会提供不同的方法来解决相同的问题,尽管有不同的优缺点。

查看ObjectOutputStream:。当你学习java时,你会发现google是你的朋友,但是你需要知道google的一些关键词