Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Reading a text file

Posted on 2011-05-11
13
Medium Priority
?
338 Views
Last Modified: 2012-05-11
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
Comment
Question by:DESEI-SELB
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 35738718
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
 

Author Comment

by:DESEI-SELB
ID: 35739127
I tried it with the txt file and the same problem.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35739247
Please say what the problem is. There should be no problem with a real text file
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 35739271
>>
/**
 * 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
 
LVL 1

Expert Comment

by:phutsa
ID: 35739769
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
 

Author Comment

by:DESEI-SELB
ID: 35740068
when I change the file to na.txt for example , I still get
0
 

Author Comment

by:DESEI-SELB
ID: 35740220
Sorry I got cut. I have enclosed a screen shot of what I see. thanks. screenshot
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35740654
>>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
 
LVL 20

Expert Comment

by:Venabili
ID: 35741036
>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
 

Author Comment

by:DESEI-SELB
ID: 35741368
thanks very much for clarification. is there any way to read a word processing file in java? thanks again.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 35741421
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35741513
Good to see you back again V!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35745644
:)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question