0

In my java application, I have to read one file. 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. 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());
    }
6
  • What is the content of hello.txt and what does the output display? Commented Dec 15, 2011 at 5:48
  • Seems like encoding issue. How was hello.txt written? Does the same problem occur with other text files? Commented Dec 15, 2011 at 5:48
  • 1
    Check the file encoding works with your system's encoding. Java should be using your default encoding. Commented Dec 15, 2011 at 5:48
  • the output I am not able to show here..that is why i could not attach..I mean it is not able to paste. yaa this seems to be some encoding issue. I have used many backward and forward slashes in the file. Commented Dec 15, 2011 at 5:51
  • What is the encoding of hello.txt? Commented Dec 15, 2011 at 5:57

5 Answers 5

1

Perhaps you have an encoding error. The constructor you are using for an InputStreamReader uses the default character encoding; if your file contains UTF-8 text outside the ASCII range, you will get garbage. Also, you don't need a DataInputStream, since you aren't reading any data objects from the stream. 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
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I do suggest using some utils for this. In commons-io for example there are IOUtils.readLines(inputStream, encoding) and IOUtils.closeQuietely(someSource)
@PetrGladkikh - Yes, utils like commons-io are good. Thanks for mentioning it.
0

The output you are getting is an ascii value ,so you need to type cast it into char or string before printing it.Hope this helps

Comments

0

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.

Examples: UTF-8, UTF-16, ... soon

Refer this Supported Encodings by Java SE 6 for more info.

6 Comments

how do you know the file is in UTF-8?
@buruzaemon: yes, since the person is not mentioned what type of encoding issue. Thats reason I mentioned basic format as "UTF-8". Based on his formatting issue, he has to change it accordingly. That's technically he has to handle. Whats wrong in my answer, which awarded as "-1"?
You do not know. But this source fragment suggest you need to specify it.
I don't know the reason for the downvote, but perhaps it's because you phrased your answer as if UTF-8 was the only possible encoding to use.
friends as per yours inputs, i have changed from "UTF-8" to encodingformat. Where the person has to change it accordingly.
|
0

My problem got solved. I dont know how. I copied the hello.txt contents to another file and run the java program. I could read all letters. dont know whats the problem in that.

Comments

0

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. This is not 100 % fool proof but works for your scenario.

Also, use of DataInputStream is unnecessary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.