简体   繁体   English

java文件读取问题

[英]java file reading issue

In my java application, I have to read one file. 在我的Java应用程序中,我必须读取一个文件。 The problem what I am facing, after reading the file, the results is coming as non readable format. 在读取文件后,我所面临的问题是结果以不可读的格式显示。 that means some ascii characters are displayed. 这意味着将显示一些ascii字符。 That means none of the letters are readable. 这意味着所有字母都不可读。 How can I make it display that? 如何显示它?

 // Open the file that is the first
        // command line parameter

        FileInputStream fstream = new FileInputStream("c:\\hello.txt");
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        // Read File Line By Line
        while ((strLine = br.readLine()) != null) {
            // Print the content on the console
            System.out.println(strLine);
        }
        // Close the input stream
        in.close();
    } catch (Exception e) {// Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }

Perhaps you have an encoding error. 也许您有编码错误。 The constructor you are using for an InputStreamReader uses the default character encoding; 您用于InputStreamReader的构造函数使用默认字符编码。 if your file contains UTF-8 text outside the ASCII range, you will get garbage. 如果您的文件包含ASCII范围之外的UTF-8文本,则会出现垃圾。 Also, you don't need a DataInputStream, since you aren't reading any data objects from the stream. 另外,由于不需要从流中读取任何数据对象,因此不需要DataInputStream。 Try this code: 试试这个代码:

FileInputStream fstream = null;
try {
    fstream = new FileInputStream("c:\\hello.txt");
    // Decode data using UTF-8
    BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
    String strLine;
    // Read File Line By Line
    while ((strLine = br.readLine()) != null) {
        // Print the content on the console
        System.out.println(strLine);
    }
} catch (Exception e) {// Catch exception if any
    System.err.println("Error: " + e.getMessage());
} finally {
    if (fstream != null) {
        try { fstream.close(); }
        catch (IOException e) {
            // log failure to close file
        }
    }
}

You have to implement this way to handle:- 您必须采用这种方式来处理:-

BufferedReader br = new BufferedReader(new InputStreamReader(in, encodingformat));

.

encodingformat - change it according to which type of encoding issue you are encounter. encodingformat根据遇到的编码问题类型进行更改。

Examples: UTF-8 , UTF-16 , ... soon 示例: UTF-8UTF-16 ,...即将推出

Refer this Supported Encodings by Java SE 6 for more info. 有关更多信息,请参考Java SE 6支持的编码

Since you doesn't know the encoding the file is in, use jchardet to detect the encoding used by the file and then use that encoding to read the file as others have already suggested. 由于您不知道文件所使用的编码,因此可以使用jchardet来检测文件所使用的编码,然后使用该编码来读取文件,就像其他人已经建议的那样。 This is not 100 % fool proof but works for your scenario. 这不是100%的傻瓜证明,但适用于您的情况。

Also, use of DataInputStream is unnecessary. 另外,不需要使用DataInputStream

您得到的输出是一个ascii值,因此您需要在将其打印之前将其类型转换为char或string。希望这会有所帮助

My problem got solved. 我的问题解决了。 I dont know how. 我不知道 I copied the hello.txt contents to another file and run the java program. 我将hello.txt内容复制到另一个文件,然后运行Java程序。 I could read all letters. 我会读所有字母。 dont know whats the problem in that. 不知道这是什么问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM