aspose file tools*
The moose likes Beginning Java and the fly likes Reading in .txt files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading in .txt files" Watch "Reading in .txt files" New topic
Author

Reading in .txt files

Leah
Greenhorn

Joined: Jul 17, 2000
Posts: 9
Hi,
I am trying to get my .txt file read into an array in my program. The file has ten lines which are tokenized into two parts with ":". The problem is that only the first line is read in and then repeated ten times instead of ten different lines being read. The same thing happens no matter what index number I use. How can I get it to read the next nine lines?? Any help is much appreciated 'cause I'm going insane!!!
Leah
PS My email addy doesn't work at the moment!
[This message has been edited by Leah (edited September 25, 2000).]
Eric Edwards
Ranch Hand

Joined: Feb 12, 2000
Posts: 60
Originally posted by Leah:
Hi,
I am trying to get my .txt file read into an array in my program. The file has ten lines which are tokenized into two parts with ":". The problem is that only the first line is read in and then repeated ten times instead of ten different lines being read. The same thing happens no matter what index number I use. How can I get it to read the next nine lines?? Any help is much appreciated 'cause I'm going insane!!!
Leah
PS My email addy doesn't work at the moment!
[This message has been edited by Leah (edited September 25, 2000).]

I didn't put mine into an array, but it reads in the entire text file, line by line, and manipulates the data. See if you can adapt this to your needs.
import java.io.*;
import java.util.*;
import java.text.*;
public class NewFormatReport
{
String item = " ";
String s = " ";
int quantity = 0;
double price = 0.00;
double value = 0.00;

public static void main(String[] args)
{
try
{
BufferedReader in = new BufferedReader(new
FileReader("C:\\price.dat"));
NewFormatReport fr = new NewFormatReport();
fr.readData(in);
}
catch(IOException e)
{
System.out.print("Error: " + e);
System.exit(1);
}
}
public void readData(BufferedReader is) throws IOException
{
while ((s = is.readLine()) != null)
{
StringTokenizer t = new StringTokenizer(s, ",");
item = t.nextToken();
quantity = Integer.parseInt(t.nextToken());
price = Double.parseDouble(t.nextToken());
value = quantity * price;
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
String fx = nf.format(value);
System.out.println(item + " " + quantity + " " + price + " " + fx);
}
}
}

Vim Win
Greenhorn

Joined: May 01, 2004
Posts: 21
Hi Leah,
You can use BufferedReader to read the lines from the text like this :
BufferedReader in
= new BufferedReader(new FileReader(filename));
This will create a buffer from wherein which you can use
in.readLine() to extract line by line as Strings (which can be stored in an array of strings). Then stored strings can be tokenized using StringTokenizer. This should work fine and I have tried doing like that.
Bye.
JVRN.
Leah
Greenhorn

Joined: Jul 17, 2000
Posts: 9
Thanks Eric and JVRN! I'll give those a whirl. The sooner I can finish this project the better!!
Leah
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading in .txt files
 
Similar Threads
reading a private method within a class
Decimals
Anyone know How to solve this basic problem?
1.4 (Say)
problem, please help