954,180 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Read and write to an ASCII Text file

I am very new to Java and I'm trying to write a Java program to read a ASCII text file, modify a field and save it. I'm not sure how to create the file definition so I can break out the individual fields. The records and fields are all fixed length.

If anyone can assist, it would be greatly appreciated.

Thanks.

Ron

Ron
Newbie Poster
1 post since Mar 2004
Reputation Points: 10
Solved Threads: 0
 

Hey,

yeah java IO is not exactly the easiest thing to learn, but you do need to know a few of the basic classes to use. The easiest way to read in an ASCII text file is to use this:

ps: make sure you catch the exceptions or else your days will be numbered believe me :)

InputStream ist = new FileInputStream(textfile);
BufferedReader istream = new BufferedReader(new InputStreamReader(ist));
String text = istream.readLine(); //just read the first line in the text file

and to break the records well i guess you have to use the StringTokenizer class,
i assume you know what that is if you don't i suggest you pay a little vivist to Java's API web page.
here's a quick look at what it would look like form here on:
StringTokenizer st;
while ( text != null )// while more line in text
{
st = new StringTokenizer(text) // break each words or whatever in to single //tokens
while ( st.hasMoreToken() ) // not sure about the spelling here
{
process each token here
}

read the next line
} // end of while ( text != null )

hope that help you get started.
take care,
Mel

Meldroz
Newbie Poster
11 posts since Mar 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You