【问题标题】:Why doesn't it read the line-termination ''\n'为什么它不读取行终止符 ''\n'
【发布时间】:2012-10-24 07:56:26
【问题描述】:

此代码正在尝试读取文件,然后将其反转为输出文件。 当它写入(不反转)时,输出是相同的。 但是当它被反转时,输出会在输出文件的 ONE 行中写入 ALL。

  int i;
    int x = 0;
    int[] ar = new int[9999];
    BufferedInputStream fin;
    BufferedOutputStream fout;
    try {

        File f1 = new File("C:/Users/NetBeansProjects/QuestionOne/input.txt");
        File f2 = new File("C:/Users/NetBeansProjects/QuestionOne/output.txt");
        fin = new BufferedInputStream(new FileInputStream(f1));
        fout = new BufferedOutputStream(new FileOutputStream(f2));


        while ((i = fin.read()) != -1) { //reads file into an array
            ar[x] = i;
            x++;
        }

        for(int y = (x-1); y >= 0; y--){ 
        //writes to a file from the end of the array
            fout.write(ar[y]);
        }

        System.out.println();
        fin.close();
        fout.close();
        } catch (FileNotFoundException e) {
        System.out.println("File is NOT found.");
    }

我正在使用BufferedInputStreamBufferedOutputStream

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE
  • 这可能是 \r\n\n 的问题。如果您正在运行 Windows,请尝试使用支持两者的阅读器(例如 Notepad++)打开它。如果我的猜测是正确的,那么它会在输出文件的角落显示 Unix。但是我将此作为评论而不是答案发布,因为您没有向我们提供足够的信息来确定。
  • 抱歉,误读了您的帖子。您可以尝试将实际文件的大小与读取的内容进行比较,以确保您首先正在读取它们。
  • @durron597 我用的是记事本,但不是记事本++
  • @InspiringProgramming:这就是问题所在,普通记事本无法正确显示\n。下载 Notepad++,你会很高兴你拥有它不仅是为了这个,而是为了一切

标签: java file fileinputstream bufferedinputstream bufferedoutputstream


【解决方案1】:

可能您正在阅读\r\n并回写\n\r。

您必须将 \r\n 作为一个单独的实体来处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 2012-09-18
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    相关资源
    最近更新 更多