Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem reading file 1

Status
Not open for further replies.

spankweat

Technical User
Jul 27, 2001
13
US
Hi,

I am having problem reading from a file. I use StringTokenizer to parse the file and find the word that I am looking for. The problem is that the word is never found; I know its there because I opened the text file.

Since the text file was generated by Windows, I think that there are some formatting issue involved. The file is PC Unicode.

I added a PrintWriter and printed each line that were read. There is a blank space between every character... (Those spaces were not visible in notepad)
I tried to look for "w o r d" (as opposed to "word") unsucessfully.

Could someone help me?
Thanks!
 
A snippet of the code that is causing the problem would be helpful.
 
you can't use a string as a delimiter with the string tokeniser. your better off looking for a substring using the String functionality.
 
What are you using as your delimiter? The default is whitespace, and that should work. Try this:

StringTokenizer st = new StringTokenizer(myString);

while (st.hasMoreTokens()){

System.out.println(st.nextToken());

}

If you use println (rather than print), and the letters come out:

w
o
r
d

Then you are splitting the tokens between each character.

Kennedy ------
KJR
 
here is the code:

import java.io.FileReader;
import java.io.LineNumberReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.*;

class FileParser {

private static String name;

public static void parse() {

try {
FileReader fr = new FileReader("test.txt");
LineNumberReader lr = new LineNumberReader(fr);
FileWriter fw = new FileWriter("testcopy.txt");
PrintWriter pw = new PrintWriter(fw,true);

boolean over = false;
do {
String line = lr.readLine();
pw.println(line);
if (line !=null) {
StringTokenizer st = new StringTokenizer(line," ");
while (st.hasMoreTokens()) {
String token = st.nextToken();
System.out.println(token);
if (token.equals("word")) {
name = st.nextToken();
System.out.println(name);
}
}
}
else over = true;
} while (!over);
lr.close();
fr.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
 
In StringTokenizer st = new StringTokenizer(line," ");
The " " is actually a tab.

I tried a space, but the System.out.println(line) doesnot give me
% w
% o
% r
% d
but
% w o r d

I am thinking there is a special character ....

Thanks!
 
Something to clarify about StringTokenizer. It doesn't work the way you think it should. Example:-

String abc = "hello world hi";
StringTokenizer stk = new StringTokenizer(abc);

If you loop through the tokens for this string, you will get "hello", "world" and "hi"

If you do this instead:-
StringTokenizer stk = new StringTokenizer(abc,"hel");

You will get "o wor", "d", "i". I hope you will get the picture about StringTokenizer.


This means that StringTokenizer doesn't suits your needs at all. You should use the method indexOf instead. So you program will be something like:-

...
String line = lr.readLine();
pw.println(line);
if (line.indexOf("word") != -1)
System.out.println(line) // this will print out the entire line that contains the word "word"
...

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Hi LeonTang,

I think that I was using StringTokenizer the right way.
I have a file containing information. After the "word", there is the information I need to save into a Java variable.
The text file might look like this; I am looking to save the String "test".
"a sentence word test"

StringTokenizer st = new StringTokenizer(line," ");
while (st.hasMoreTokens()) {
String token = st.nextToken();
System.out.println(token);
if (token.equals("word")) {
name = st.nextToken();
System.out.println(name);
}

Then name = "test";


 
I used a PrintWriter to write what the program was reading from the original file.

When I opened that file in Word and WordPad, I noticed that there was a weird ASCII character between every character.
It looked like a box. I opened the text file in TextPad and tried to copy the weird character, so that I can add it to the program but I received a "cannot copy null character".

The character was not present in the original file.
 
Sorry to have misunderstood what you are trying to do. But anyway, it seems like the problem lies in the text file rather than your codes. You codes should be working fine. Perhaps you can send me the text file so that I can take a look at it?

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
spankweat, i ran your code through a debugger and it works fine. it parses the string and finds the word "word", the only problem is it continues parsing the string even after the word has been found. if you are searching for "word" more than once in the file, this is fine. do u want to do that??

if not i would suggest the following code changes:

import java.io.FileReader;
import java.io.LineNumberReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.*;

class FileParser
{

public static void main( String[] args )
{
parse();
}

private static String name;

public static void parse()
{

try
{
FileReader fr = new FileReader("c:/test.txt");
LineNumberReader lr = new LineNumberReader(fr);
FileWriter fw = new FileWriter("c:/testcopy.txt");
PrintWriter pw = new PrintWriter(fw,true);

boolean over = false;

do
{

String line = lr.readLine();
pw.println(line);

if (line !=null)
{

StringTokenizer st = new StringTokenizer(line," ");

// Also set your exit condition here
while( ( st.hasMoreTokens() ) && ( !over ) )
{
String token = st.nextToken();
System.out.println(token);

if( token.equals( "word" ) )
{
name = st.nextToken();
System.out.println( name );

// Ensure you stop after you
// have found your word
over = true;
}
}
}
else
{
over = true;
}
} while (!over);

lr.close();
fr.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
}
 
Hi LeonTang and LittleWing,

I think that the problem is definitely coming from the text file. LittleWing, thanks for the rectification.
If you have a Windows OS, run:
msinfo32 /report test.txt
test.txt is the file I am trying to parse.
As you probably know, it contains information about the computer you are using. I would like to save this information because I dont like the MS viewer for the nfo files.

Try to parse test.txt. Thanks for helping!
 
i tried the msinfo32 /report test.txt and it didn't work. i am running w2k.

i created a text file myself and ran your program against it. it ran fine. it must be the ms format of the file. is it possible to view that file in a simple text editor???
 
the problem is inthe formatting in the file produced by msinfo. it isn't readable by your stream...am on the case!! :)
 
i'm not entirly hapy with this solution. it should be possible to sort out this sort of file encoding using the io streams but so far it isn't working. so as a quick and dirty solution, i fixed th eproblem my self. this works. if i get the proper solution, i'll post, in the mean time, this works a treat!!

import java.io.FileReader;
import java.io.LineNumberReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.*;

class FileParser
{

public static void main( String[] args )
{
parse();
}

private static String name;

/**
* This method removes all chars with the ascii value of 0(zero)
*
* @param s A StringBuffer with the text
*
* @return String representation of the purged string
*/
public static String stripString( StringBuffer s )
{

char c;

// Loop through the string
for( int i = 0; i < s.length(); i++ )
{

// Read a char
c = s.charAt( i );

// if this char is an addition space
if( ( int )c == 0 )
{
// Remove it
s.deleteCharAt( i );
}
}

// Return a string
return s.toString();
}

public static void parse()
{

try
{

FileReader fr = new FileReader(&quot;c:/test.txt&quot;);
LineNumberReader lr = new LineNumberReader(fr);

FileWriter fw = new FileWriter(&quot;c:/testcopy.txt&quot;);
PrintWriter pw = new PrintWriter(fw,true);

boolean over = false;

String line;

do
{

line = stripString( new StringBuffer( lr.readLine() ) );
pw.println( line );

if (line !=null)
{

StringTokenizer st = new StringTokenizer( line );

// Also set your exit condition here
while( ( st.hasMoreTokens() ) && ( !over ) )
{
String token = st.nextToken();
// System.out.println(token);

if( token.equals( &quot;Word&quot; ) )
{
name = st.nextToken();
System.out.println( name );

// Ensure you stop after you
// have found your word
over = true;
}
}
}
else
{
over = true;
}
} while (!over);

lr.close();
fr.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
}

enjoy.
 
Thanks a lot LittleWing.
That's a really smart solution! Let me know if you find another solution.
I was wondering if you could use another type of stream to handle this type of file ...

By the way there is a faster way to get the text file
msinfo32 /report test.txt /categories +SystemSummary
 
i originally used a fileinputstream, along with others but chaged it back to to the filereader cause thats what u were using.
 
I meant a stream to directly pull up the information from the text file (without having to delete the null char) =)
A stream that would skip the null char ...
 
i had this problem the very first time i wrote a java program, 4 years ago, and there was a way around this....but i just can't remember what it was :-(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top