java - 为什么这读不进去?

标签 java string io

我知道我没有做正确的事情。我知道该文件需要可序列化才能读取文本文件。 我在主类上实现了可序列化。但我的 readText 和 writeText 没有转换。
当我读取时没有任何内容进入,而当我写出时,文件不是文本。

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

最佳答案

Nothing is coming in

你永远不会调用read.add(line)并且您尝试在 catch block 内的无限循环中读取文件,只有在您无法读取文件时才会进入该循环。

只使用一个try block ,意思是尝试一次打开并读取文件,否则,如果无法打开文件,就没有理由继续尝试读取文件

 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;

现在,无论您对这个序列化对象做什么,它都是完全独立的,并且不需要设置为可序列化的不是文件或主类,而是您将使用 writeObject 的任何对象。方法上。但是,您正在读取和写入已经可序列化的 String 对象。

when I write out the file is not text

不确定你所说的非文本是什么意思,但是如果你按照上面的代码操作,你将得到初始文件中的确切内容......无论如何,你不需要 write列表变量。
您必须使用 ArrayList<String> file 的各个行参数,但不是 file.toString()

for (String line:file) {
    out.println(line);
}
out.close(); // always close your files and writers 

关于java - 为什么这读不进去?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49832294/

相关文章:

java - 我的函数中出现 NullPointerException

JAVA:使用计数器格式化字符串

javascript - 生成不含 O 和 0 的大写字母和数字的随机字符串

C Sprintf 格式错误

c# - 执行File.Delete后,文件仍处于DELETE PENDING

c++ - 使用 stringstream 和 ofstream 写长日志文件

java - 通过使用<img th:src = “@{/images/anabada.jpeg}”无法加载thyemleaf中的图像

Java在ubuntu上找不到文件

multithreading - Python 类管道流对象

java - 如何在java中使用apache SPARK分割和过滤字符串