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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using File Reader

Status
Not open for further replies.

simdan42

Programmer
Jul 17, 2002
118
0
0
US
How can I use file reader to read a file line by line and delimited by "|" into variables in the program?
 
You can develop your own class that extends the any reader class and then override the methods that you wnat to use.
But u will have to develop your own class.
 
Lets say your file looks like this

a|aa|aaa
b|bb|bbb
c|cc|ccc

then the output will be like

aaaaaa
bbbbbb
cccccc

or was it something else you wanted ?


import java.io.*;
import java.util.*;

public class Driver
{
public static void main(String[] arg) throws IOException
{

StringBuffer data = new StringBuffer();

BufferedReader reader = new BufferedReader(new FileReader(new File("data.txt")));

String tmp = reader.readLine();

while(tmp != null)
{
StringTokenizer strTok = new StringTokenizer(tmp,"|");

//Saving the data into the StringBuffer
//You don't have to do this, it dependes
//on what you want to to with your data
while(strTok.hasMoreTokens())
{
data.append(strTok.nextToken());
}
data.append("\n");
tmp = reader.readLine();
}

//Print the result
System.out.print(data);

}

 
This is what I have but it is not reading the file. If I hard code a value for tmp it works perfectly!

public void TextToDB() throws Exception
{
//Create text file objects.


String ProductId_Hold;
String ProductDesc_Hold;
String QtyOnHand_Hold;
String QtyOrd_Hold;
String ExtPrice_Hold;
String UnitPrice_Hold;


ProductId_Hold = "";
ProductDesc_Hold= "";
QtyOnHand_Hold= "";
QtyOrd_Hold= "";
ExtPrice_Hold= "";
UnitPrice_Hold= "";

lblMessage.setText("Before Try");

try
{

StringBuffer data = new StringBuffer();


BufferedReader reader = new BufferedReader(new FileReader(new File("DBFile.dat")));

String tmp = reader.readLine();

while(tmp != null)
{

lblMessage.setText("1St While");

StringTokenizer strTok = new StringTokenizer(tmp,"|");

//Saving the data into the StringBuffer
//You don't have to do this, it dependes
//on what you want to to with your data
while(strTok.hasMoreTokens())
{
lblMessage.setText("2nd While");

ProductId_Hold = strTok.nextToken();
ProductDesc_Hold= strTok.nextToken();
QtyOnHand_Hold= strTok.nextToken();
QtyOrd_Hold= strTok.nextToken();
UnitPrice_Hold= strTok.nextToken();
ExtPrice_Hold= strTok.nextToken();

cmdProduct.executeUpdate("Insert Into Product "
+ "([ProductID], [ProductDesc], [QtyOnHand], [QtyOrd], [UnitPrice], [extPrice] ) "
+ "Values('"
+ ProductId_Hold + "', '"
+ ProductDesc_Hold + "', '"
+ QtyOnHand_Hold + "', '"
+ QtyOrd_Hold + "', '"
+ UnitPrice_Hold + "', '"
+ ExtPrice_Hold + "')");

//Add to name list
lblMessage.setText("Import Sucessful");
}
tmp = reader.readLine();
lblMessage.setText("TMP");
}


}

catch(SQLException error)
{
lblMessage.setText("Error during Edit. " + "Error: " + error.toString());
}

catch(Exception error)
{
lblMessage.setText("Error during Edit. " + "Error: " + error.toString());
}

}
 
So what is the difference between the two values of tmp when you hard code it and when you use readLine()?

-pete
 
No errors, It is as thought it is not reading from the file. I put a line of code in there to display the value of tmp, but it was null.
 
OK Guys, and Girls I got it.

I had

BufferedReader reader = new BufferedReader(new FileReader(new File("DBFile.dat")));

And changed it to

BufferedReader reader = new BufferedReader(new FileReader(new File("C:\\DBFile.dat")));

And It worked! Thank you for all your help!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top