File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes StringTokenizer Class...... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "StringTokenizer Class......" Watch "StringTokenizer Class......" New topic
Author

StringTokenizer Class......

Rekha Suri
Greenhorn

Joined: Mar 22, 2000
Posts: 15
Hi, the following code has to read a file, catch the "Get"phrase in the file, and copy each and every line containing "Get" into another file. The other code is untouched. I could read only upto 11 lines of the source file, whereas it actually contains about 100 lines. Am I missing out on something ? Any clarifications and comments would be gladly appreciated.
Thanks.
===============================================================
import java.io.*;
import java.util.*;
public class readSet
{
public static void main(String[] args)
{
if (args.length != 2)
System.out.println("Enter source AND destination filenames.");
else
readFile(args[0],args[1]);
}
public static int readFile(String source,String destination)
{
FileInputStream fis = null;
DataInputStream in = null;
FileOutputStream fos = null;
DataOutputStream dos = null;
StringTokenizer strToken = null;
String line = null;
int count = 0, cntoken = 0;
try
{
// Create file input stream
fis = new FileInputStream(source);
// Redirect stream through a DataInputStream
in = new DataInputStream(fis);
fos = new FileOutputStream(destination);
dos = new DataOutputStream(fos);
// Read the first line.
line = in.readLine();
strToken = new StringTokenizer(line,"Get",true);
// Obtain the (system dependent) line separator characters.
String lineSep = System.getProperty("line.separator");
while (strToken.hasMoreTokens())
{
if (line != null)
{
count = count + 1;
dos.writeBytes(line + lineSep);
line = in.readLine();
strToken.nextToken();
}
else
{
System.out.println("End of source file reached.");
break;
}
} // while ends when the next line is null in the file.
System.out.println("Number of tokens:" + count);
}// try ends.
catch (EOFException eofe)
{
System.out.println("End of file encountered.");
// Do nothing on end of file.
}
catch (FileNotFoundException fnfe)
{
// No such file.
System.err.println("File not found :" + source);
}
catch (IOException ioe)
{
// On other exceptions print error message.
System.out.println(ioe.getMessage());
}
catch (NumberFormatException Nfe)
{
System.out.println(Nfe.getMessage());
}
finally
{
// ALWAYS close the streams when done with them.
try
{
System.out.println("Number of Get tags in this file :" + count);if (fis != null) fis.close();
if (in != null) in.close();
if (dos != null) dos.close();
if (fos != null) fos.close();
}
catch (IOException ioe)
{
}
}
return count;
}// method readFile() ends.
} // class readSet ends.
Pawel Veselov
Ranch Hand

Joined: Jan 14, 1999
Posts: 165
I've found two major problems for that code.
The delimeter argument for Tokenizer is not a delimeter string, but a set of delimeter charaters. So, when you ask
to create tokenizer (String, "Get", true), it will break at
every 'G', 'e' and 't' character until the string is completed.
I think what you need is startsWith() method from java/lang/String.
More to say, you using hasMoreTokens() return value to complete the while{} loop, without reseting the tokenizer to newly received string. So, you've done when the first string has no more tokens it it.
To check the file is completed, you should use eof() method.

With best of best regards, Pawel S. Veselov ( aka Black Angel )
Rekha Suri
Greenhorn

Joined: Mar 22, 2000
Posts: 15
Thanks a lot Pawel. I'll implement the corrections and work it out. I need some more clarification. The source file has all method calls, like
long GetAddress();
String SetMail();
and so on, so the major thing to be achieved here is to separate the Get and Set methods. If I use startsWith() method of String class, then the line that is read from the source file has to have Get in the first index position(default) or in the specified position(we have to know the index of Get in each and every string for this.) Won't this complicate it?
Am I thinking in the right lines ? Please, any suggestions will be helpful.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Instead of startsWith(String), use indexOf(String) - it will return -1 if the substring is not found in the target String.
In order to split the file into lines, you could also use a LineReader wrapped around a FileReader. Then you just keep calling String line = readLine() until it returns null, indicating end of file.
You may also want to consider whether the case should be ignored - should you find "get", "GET", "gEt" etc. as well? If so, a simple solution is to convert each line toUpperCase() (or toLowerCase()) before you search it.

"I'm not back." - Bill Harding, Twister
Rekha Suri
Greenhorn

Joined: Mar 22, 2000
Posts: 15
Thanks a lot Jim and Pawel. Your suggestions were really helpful. There are so many ways to solve a problem, I guess choosing among them comes from experience.
Rishi Singh
Ranch Hand

Joined: Dec 09, 2000
Posts: 321
hai rekha,
suppose I have a blank line in my text file from where I am reading and after 1/2 blank lines I have something more to read how to avoid that.It gives me null pointer exception!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringTokenizer Class......