1

嗨,我在将输入的数据附加到二进制文件的末尾时遇到了麻烦,在查找了如何做到这一点后,我在这里找到了堆栈溢出的解决方案:

try {
    ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream ("BinaryWrite.hagl", true));
    out.writeObject(allTowns);
    out.flush ();
    }
catch (Exception e){
    System.out.println("IMPOSSSIBLE");
    }

在这段代码中,allTowns 是我希望添加的数据所在的数组。我遇到的问题是,当我运行我的程序并在最后显示文件中的内容时,这段代码根本不会写入文件,我想知道是否有人可以帮助我理解为什么这不起作用或甚至只是在必要时推荐一种不同的方法。

我的完整代码(这部分目前已被注释掉,因此可以轻松创建文件):

    import java.util.*;
    import java.io.*;
 public class CoastaslTowns implements Serializable
{

public static Scanner input = new Scanner(System.in);

String name, county;
int population, area;
public static int count = 0;
public static int continuation = 0;
public static CoastaslTowns[] allTowns = new CoastaslTowns[50];
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int loop1 = 0;
    for (int i=0; i < 50; i++) {
        allTowns[i] = new CoastaslTowns();
    }

    while (loop1 == 0) {

        System.out.println("Please enter the name of the town.");
        String nameEntered = input.nextLine();

        System.out.println("Please enter the county in which the town resides.");
        String countyEntered = input.nextLine();

        System.out.println("Please enter the population of the town.");
        int populationEntered = input.nextInt();

        System.out.println("Please enter the area of the town.");
        int areaEntered = input.nextInt();
        input.nextLine();
        System.out.println("Are you satisfied with your entries?");
        String satisfaction = input.nextLine();
        if (satisfaction.equals("yes")) {
            loop1 = 5; 
            System.out.println("Thank you for entering your details");
            continuation = 1;

        }
        else if (satisfaction.equals("no")) {
            System.out.println("Would you like to continue and enter more towns?");
            String countychecker = input.nextLine();
            if (countychecker.equals("yes")) {

            }
            else {
                break;
            }
        }
        writeToFile(nameEntered, countyEntered, populationEntered, areaEntered); 
    }   
    ReturnTowns();
}

public static void inputVariations(){
}

public static void writeToFile(String nameEntered, String countyEntered, int populationEntered, int areaEntered) {


    int loop2 = 0;
    while (loop2 == 0){
        allTowns[count].population = populationEntered;
        allTowns[count].name = nameEntered; 
        allTowns[count].county = countyEntered;
        allTowns[count].area = areaEntered;
        if (continuation == 1) {
            loop2 = 1;

        }
        else {
            loop2 = 1;
        }
        count = count + 1;
    }

    try {
        FileOutputStream fileOut = new FileOutputStream("BinaryWrite.hgal");
        ObjectOutputStream out = new ObjectOutputStream(fileOut);
        out.writeObject(allTowns);
        out.close();
        fileOut.close();
    } catch (IOException i) {}

    /*
    try {
    ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream ("BinaryWrite.hagl", true));
    out.writeObject(allTowns);
    out.flush ();
    }
    catch (Exception e){
    System.out.println("IMPOSSSIBLE");
    }
     */

}
public static void ReturnTowns(){
    {

        int x = 0;
        CoastaslTowns[] bw = null;
        try {
            FileInputStream fileIn =
                new FileInputStream("BinaryWrite.hgal");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            bw = (CoastaslTowns[]) in.readObject();

            while (bw[x].population != 0) {
                System.out.println(bw[x].name);
                System.out.println(bw[x].county);
                System.out.println(bw[x].population);
                System.out.println(bw[x].area);
                System.out.println();
                x++;

            }

            in.close();
            fileIn.close();
        } catch (IOException i) {
        } catch (ClassNotFoundException c) {
        }
    }
}

} 

任何帮助将不胜感激。

4

0 回答 0