尝试使用Java打印文件内容时获取垃圾字符

时间:2022-01-16 21:51:24

I am reading a file's contents and trying to print the contents using java. But it prints junk characters along with the file content.

我正在读取文件的内容并尝试使用java打印内容。但它会打印垃圾字符以及文件内容。

Code:

import java.io.*;

public class ReadFile {

    public String readFile(String filePath){

        StringBuilder contents = new StringBuilder();
        File file = new File(filePath);


        try{
            String lines = null;
            FileReader fileReader1 = new FileReader(file);
            BufferedReader buffer = new BufferedReader(fileReader1);

            while((lines = buffer.readLine())!=null){
                contents.append(lines);

            }
            buffer.close();
        }
        catch(FileNotFoundException ex){
                System.out.println("File not found.");
        }catch(IOException ex){
            System.out.println("Exception ocurred.");
        }
        return contents.toString();
    }

    public static void main(String[] args){

        ReadFile rf = new ReadFile();
        String lines = rf.readFile("C:\\Data\\FaultDn.txt");

        System.out.println("Original file contents: " + lines);


    }
}

The file contents are:

文件内容是:

partner.cisco.com:org-root/mac-pool-QA_MAC_Pool_5-Sep-2012_12:00

The output is:

输出是:

"Original file contents: ÿþp ..." and then junk characters after every letter.

Can you please point me to what I am missing in this code?

能否请您指出我在此代码中缺少的内容?

4 个解决方案

#1


0  

The only explanations I can think of are:

我能想到的唯一解释是:

  • you are reading a different file to the one you think you are reading, or
  • 您正在阅读与您认为正在阅读的文件不同的文件,或者

  • the program you are executing doesn't match the source code above; e.g. you are executing an old version of the class file.
  • 您正在执行的程序与上面的源代码不匹配;例如您正在执行旧版本的类文件。

#2


0  

Your post is misleading as most of us understood that the output your program is generating is

您的帖子具有误导性,因为我们大多数人都明白您的程序产生的输出是

![Alt Output](C:\Share\Output.jpg)

If the what you are reading contains junk characters then it is most likely an encoding issue. How was the original file generated?

如果您正在阅读的内容包含垃圾字符,则很可能是编码问题。原始文件是如何生成的?

#3


0  

I also tried you'r code. It is working correctly. but I used Ubuntu so I only changed you file path. so sometimes error occurred in you'r file path or in your file.

我也试过你的代码。它工作正常。但我使用Ubuntu所以我只改变了你的文件路径。所以有时在你的文件路径或文件中发生错误。

#4


0  

Issue resolved. I have corrected my powershell script by adding ASCII encoding as follows:

问题解决了。我通过添加ASCII编码更正了我的powershell脚本,如下所示:

Out-File -Encoding ASCII 

and I have used

我已经习惯了

System.getProperty("line.separator");

This resolves the issue and prints the file contents as it is (including new lines)

这解决了问题,并按原样打印文件内容(包括新行)

#1


0  

The only explanations I can think of are:

我能想到的唯一解释是:

  • you are reading a different file to the one you think you are reading, or
  • 您正在阅读与您认为正在阅读的文件不同的文件,或者

  • the program you are executing doesn't match the source code above; e.g. you are executing an old version of the class file.
  • 您正在执行的程序与上面的源代码不匹配;例如您正在执行旧版本的类文件。

#2


0  

Your post is misleading as most of us understood that the output your program is generating is

您的帖子具有误导性,因为我们大多数人都明白您的程序产生的输出是

![Alt Output](C:\Share\Output.jpg)

If the what you are reading contains junk characters then it is most likely an encoding issue. How was the original file generated?

如果您正在阅读的内容包含垃圾字符,则很可能是编码问题。原始文件是如何生成的?

#3


0  

I also tried you'r code. It is working correctly. but I used Ubuntu so I only changed you file path. so sometimes error occurred in you'r file path or in your file.

我也试过你的代码。它工作正常。但我使用Ubuntu所以我只改变了你的文件路径。所以有时在你的文件路径或文件中发生错误。

#4


0  

Issue resolved. I have corrected my powershell script by adding ASCII encoding as follows:

问题解决了。我通过添加ASCII编码更正了我的powershell脚本,如下所示:

Out-File -Encoding ASCII 

and I have used

我已经习惯了

System.getProperty("line.separator");

This resolves the issue and prints the file contents as it is (including new lines)

这解决了问题,并按原样打印文件内容(包括新行)