user414967
user414967

Reputation: 5325

java file reading issue

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());
    }

Upvotes: 0

Views: 298

Answers (5)

Aravind Yarram
Aravind Yarram

Reputation: 80192

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.

Upvotes: 0

user414967
user414967

Reputation: 5325

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.

Upvotes: 0

Siva Charan
Siva Charan

Reputation: 18064

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.

Upvotes: 0

Abhijit C
Abhijit C

Reputation: 313

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

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234857

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
        }
    }
}

Upvotes: 1

Related Questions