【问题标题】:Why is this not reading in?为什么这不读入?
【发布时间】:2018-09-24 17:11:47
【问题描述】:

我知道我没有正确地做某事。我知道该文件需要可序列化才能读取文本文件。 我在主类上实现了 Serializable 。但是我的 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) {
        } 
    }
}

【问题讨论】:

  • 您的 while 循环在 catch 块内...
  • 不清楚您要做什么。将文件逐行读入列表?这里已经回答了stackoverflow.com/q/5343689/2308683
  • 你需要在while循环之前关闭IOException e1catch
  • 它是加密和解密文件的大型程序的一部分。您读入文本文档,打印出来,加密和打印,将加密文件保存到文本文件,从内存中清除它,读入加密文本文件,解密文件,将解密文件写出到控制台然后就完成了。这是一个 ArrayList,我必须打开一个对话框来引入一个文本文件(原始文件和加密版本)。这说明清楚了吗?

标签: java string io


【解决方案1】:

什么都没有进来

您从未调用过read.add(line),而是尝试在 catch 块内的无限循环中读取文件,只有在您无法读取文件时才输入该文件 .

只需使用一个try块,即尝试一次打开并读取文件,否则如果无法打开文件,则没有理由继续尝试读取文件

 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;

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

当我写出文件时不是文本

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多