将对象写入文件" Persons.obj"

时间:2014-12-15 12:59:48

标签: java file bufferedreader filereader printwriter

你好伙伴Stackoverflowers。我有一个关于Java的问题,我应该将所有的对象p1 - p4放在一个名为“personen.obj”的文件中,对于p1,将所有数据写入“persoon1.obj”等。

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

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

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

1 个答案:

答案 0 :(得分:0)

为了简单地将少量字符串写入文件,我建议使用“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;
}

还值得了解编码。 某些系统可以/将以不同的方式编写文本文件,在大多数小的情况下,它不是问题,但可能是潜在的。

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

同时确保给其他网站一个读取,因为大多数网站会提供不同的方法来解决同样的问题,尽管有不同的优点和缺点。