java - PrintWriter 不保存所有字符

标签 java file input output printwriter

class Start 
{
    File plikIN;
    Scanner in;
    PrintWriter out;

    Start(String input, String output)
    {
        plikIN = new File(input);
        try{
        in = new Scanner(plikIN);
        out = new PrintWriter(output);
        } catch (FileNotFoundException e) {System.out.println("Nie odnaleziono podanego pliku\n"+e);}
    }

    private void saveing() throws IOException
    {
        String word;
        int wordLength;
        String wordTable[];
        char c;

        while((word = in.next()) != null)
        {
            wordLength = word.length();
            wordTable = new String[wordLength];
            for(int k=0; k<wordTable.length; ++k)
            {
                c = word.charAt(k); 
                out.println(c);
            }   
        }
        out.close();
    }

    public static void main(String[] args) throws IOException
    {
        String nazwaPlikuWejsciowego = args[0];
        String nazwaPlikuWyjsciowego = args[1];
        Start doit = new Start(nazwaPlikuWejsciowego, nazwaPlikuWyjsciowego);
        doit.saveing();

    }
}

我的问题是保存到文件。经过上面的save方法后,文件不包含任何单个字符。例如,当我将 out.close() 移动到 while 时,该文件包含一个单词。当out.close()for时,程序只保存一个字符。为什么?

最佳答案

out.close() 之前添加 out.flush()

您需要在关闭之前将字节刷新到磁盘..

关于java - PrintWriter 不保存所有字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28004074/

相关文章:

file - 如何使用 AppleScript 在桌面上的文件夹中创建新文件?

java - 如何读取保存编码的文件?

java - 使用 Java 在 for 循环之外返回值时遇到问题

c - 从文件中读取数据到结构数组中

java - 如何在 Java 中使用 Scanner 读取文本文件中的特定字符?

c - 逐字阅读,包括后面的空格

c - 在C中读入并记录一个数字

python - While 循环不断提示用户提供正确的输入类型

java - 调用一个方法时调用另一个方法?

java - 来自父类(super class)的 protected 变量对于不同包中的子类不可见