1

I know I'm not doing something correctly. I know the file needs to be Serializable to read a text file. I've got implements Serializable on the main class. But my readText and my writeText aren't converting.
Nothing is coming in when I read and when I write out the file is not text.

public static ArrayList<String> readText()  {
    ArrayList<String> read = new ArrayList<String>();
    Frame f = new Frame();
    FileDialog foBox = new FileDialog(f, "Reading serialized file",
            FileDialog.LOAD);
    foBox.setVisible(true);

    String foName = foBox.getFile();
    String dirPath = foBox.getDirectory();
    File inFile = new File(dirPath + foName);
    BufferedReader in = null;

    ObjectInputStream OIS = null;

    try {
        in = new BufferedReader(new FileReader(inFile));
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }

    String line = null;
    try {
        line = in.readLine();
    } catch (IOException e1) {

        e1.printStackTrace();

        while (line != null) {
            try {
                FileInputStream IS = new FileInputStream(inFile); 
                OIS = new ObjectInputStream(IS); 

                inFile = (File) OIS.readObject();

            } catch (IOException io) {
                io.printStackTrace(); 
                System.out.println("An IO Exception occurred");
            }

            catch (ClassNotFoundException cnf) {
                cnf.printStackTrace(); // great for debugging!
                System.out.println("An IO Exception occurred");
            } finally 
            {
                try {
                    OIS.close();
                } catch (Exception e) {
                } 

            }
        }
    }

    return read;
}


public static void writeText(ArrayList<String> file) {
    ArrayList<String> write = new ArrayList<String>();

    Frame f = new Frame();

    FileDialog foBox = new FileDialog(f, "Saving customer file",
            FileDialog.SAVE);
    foBox.setVisible(true);

    String foName = foBox.getFile();
    String dirPath = foBox.getDirectory();

    File outFile = new File(dirPath + foName);

    PrintWriter out = null;

    try {
        out = new PrintWriter(new BufferedWriter(new FileWriter(outFile)));

        for (int i = 0; i < write.size(); i++) {
            String w = write.get(i);
            out.println(file.toString());
        }
    }

    catch (IOException io) {
        System.out.println("An IO Exception occurred");
        io.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (Exception e) {
        } 
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Amanda
  • 11
  • 1
  • 2
    Your while loop is inside the catch block... – OneCricketeer Apr 14 '18 at 14:10
  • It's not exactly clear what you're trying to do. Read a file line by line into a list? That's been answered here https://stackoverflow.com/q/5343689/2308683 – OneCricketeer Apr 14 '18 at 14:18
  • You will need to close `IOException e1` `catch` prior to `while` loop. – Allen King Apr 14 '18 at 14:51
  • It's part of a larger program that encrypts and decrypts a file. You read in a text document, print it out, encrypt and print, save the encrypted file out to a text file, clear it from the memory, read in the encrypted text file, decrypt the file, write out the decrypted file to the console and then it's done. This is an ArrayList that I have to open a dialogue box to bring in a text file (both the original and then the encrypted version). Does that clarify? – Amanda Apr 14 '18 at 14:54

1 Answers1

0

Nothing is coming in

You're never calling read.add(line) and you're attempting to read the file within an infinite loop inside of the catch block, which is only entered if you are not able to read the file.

Just use one try block, meaning try to open and read the file at once, otherwise, there's no reason to continue trying to read the file if it's not able to be opened

 List<String> read = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(inFile)) {
    String line = null;
    while ((line = in.readLine()) != null) {
        read.add(line);  // need this 
    } 
} catch (Exception e) {
    e.printStackTrace();
}
return read;

Now, whatever you're doing with this serialized object stuff, that's completely separate, and it isn't the file or your main class that needs set to Serializable, it's whatever object you would have used a writeObject method on. However, you're reading and writing String objects, which are already Serializable.

when I write out the file is not text

Not sure what you mean by not text, but if you followed the above code, you'll get exactly what was in the initial file... Anyway, you do not need a write list variable.
You must use the individual lines of ArrayList<String> file parameter instead, but not file.toString()

for (String line:file) {
    out.println(line);
}
out.close(); // always close your files and writers 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245