Link to home
Start Free TrialLog in
Avatar of DESEI-SELB
DESEI-SELB

asked on

Reading a text file

Hi experts,

I am not sure why I am getting all the text  letters when just reading a file. this is the example:

stuff lik:  €
import java.io.*;

/**
 * This program reads a text file line by line and print to the console. It uses
 * FileOutputStream to read the file.
 *
 */
public class FileInput {

      public static void main(String[] args) {

            File file = new File(
                        "V:\\WebSphere Support\\WAS Environment and POC\\POContact list\\NA.doc");
            StringBuffer contents = new StringBuffer();

            BufferedReader reader = null;
            try {

                  reader = new BufferedReader(new FileReader(file));

                  String text = null;

                  // repeat until all lines is read

                  while ((text = reader.readLine()) != null) {

                        contents.append(text).append(System.getProperty(

                        "line.separator"));

                  }

            } catch (FileNotFoundException e) {

                  e.printStackTrace();

            } catch (IOException e) {

                  e.printStackTrace();

            } finally {
                  ;
                  try {

                        if (reader != null) {
                              System.out.println("reader is not null");

                              reader.close();

                        }

                  } catch (IOException e) {

                        e.printStackTrace();

                  }

            }
            System.out.println(contents.toString());

      }
}

thanks,
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DESEI-SELB
DESEI-SELB

ASKER

I tried it with the txt file and the same problem.
Please say what the problem is. There should be no problem with a real text file
>>
/**
 * This program reads a text file line by line and print to the console. It uses
 * FileOutputStream to read the file.
 *
 */
>>

Is a perfect description of what the program will do with a real text file (apart from the bit about FileOutputStream - that's for writing, not reading)
This code is read only text file .
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;


public class readfile {

	
	public static void main(String[] args) {
		try {
			FileReader rd = new FileReader("test.txt");     //file test.txt you can use your text file to replace it
			BufferedReader bf = new BufferedReader(rd);
			String txt="",data="";
			
			while ((txt=bf.readLine())!=null) {
				data+=txt+"\n";
			}
			System.out.println(data);
		} catch (Exception e) {	}
		
	}
}

Open in new window

when I change the file to na.txt for example , I still get
Sorry I got cut. I have enclosed a screen shot of what I see. thanks. User generated image
>>when I change the file to na.txt for example

You misunderstand: the name of the file is insignificant. What is significant is the content of the file. You are attempting to read a word processing (binary) file with the Java API. That can't be done. (See my previous comments)
>Sorry I got cut. I have enclosed a screen shot of what I see. thanks.

This is what you will see if you open it with Notepad as well. Why do you expect Java to do something different?

As CEHJ said more than  once - if you want to test a program that reads text files, you need a text file which Notepad can open and read properly (with some restrictions about what Notepad can and cannot do actually but that is irrelevant). If you want to read a specific format, you cannot do it line by line... you need an appropriate API (as already mentioned)
thanks very much for clarification. is there any way to read a word processing file in java? thanks again.
CEHJ answered that in the very first comment:
"You need the appropriate API, such as POI, OpenOffice UNO API etc."

http://www.hiteshagrawal.com/java/reading-word-document-in-java is an example with POI for example (and this is the API http://poi.apache.org/ )
Good to see you back again V!
:)