• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

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,
0
DESEI-SELB
Asked:
DESEI-SELB
  • 6
  • 4
  • 2
  • +1
1 Solution
 
CEHJCommented:
You can't read a binary file like an MS .doc file with Java. You need the appropriate API, such as POI, OpenOffice UNO API etc.
0
 
DESEI-SELBAuthor Commented:
I tried it with the txt file and the same problem.
0
 
CEHJCommented:
Please say what the problem is. There should be no problem with a real text file
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
CEHJCommented:
>>
/**
 * 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)
0
 
phutsaCommented:
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

0
 
DESEI-SELBAuthor Commented:
when I change the file to na.txt for example , I still get
0
 
DESEI-SELBAuthor Commented:
Sorry I got cut. I have enclosed a screen shot of what I see. thanks. screenshot
0
 
CEHJCommented:
>>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)
0
 
VenabiliCommented:
>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)
0
 
DESEI-SELBAuthor Commented:
thanks very much for clarification. is there any way to read a word processing file in java? thanks again.
0
 
VenabiliCommented:
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/ )
0
 
CEHJCommented:
Good to see you back again V!
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now