Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

trying ot read a line but instead reading a file  RSS feed

 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I'm trying to read a file line by line and then check for instances of the line for display. Well it will read the file in but instead of reading from the line that I want it just reads from the first line...here is a sample of what I have:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
public class testImporter{

String LineID;

public static void main (String args[]) throws Exception {
File file = new File("C:\\Data");
if (!file.exists()) {
System.out.println("Can't read " + file);
}
else {
if (file.isDirectory( )) {
String [] files = file.list( );
for (int i=0; i< files.length; i++) {
String ifile = files[i];
doit (file + File.separator + ifile);
}
}
else {
doit(args[0]);
}
}
}
public static void doit (String ifile) throws Exception {

String lineID1 = "GS";

method12(ifile,lineID1);
}
public static void method12(String ifile, String lineID1) throws Exception {
BufferedReader inData = new BufferedReader(new InputStreamReader(new FileInputStream(ifile)));

String newLine ="\012";
String line = "";
String str1 = "";
String str2 = "";
String fLine = "";

boolean firstTime = true;

while ((line = inData.readLine()) != null) {

fLine = fLine + (firstTime?"":newLine) + line;
if (firstTime) firstTime = false;
}
inData.close();
if (fLine.indexOf(lineID1) >= 0) {

StringTokenizer st = new StringTokenizer(fLine, "|");
String GS = st.nextToken();
String locat2 = st.nextToken();
String Shipto = st.nextToken();
String Shipfrom = st.nextToken();
String locat5 = st.nextToken();
String locat6 = st.nextToken();
String locat7 = st.nextToken();
String locat8 = st.nextToken();
String locat9 = st.nextToken();

System.out.println("there is:" + Shipto);
System.out.println("there is:" + Shipfrom);
}
}
}
This compiles and runs but instead of reading from the second line (where lineID1 is) it just takes the data from the first line. Any Ideas on how to fix this?
 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
side note to this...the lines are seperated by a "~"
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris:
Which line do you want to read? If just the first line, then it's fine. If you want to read only second line, then just call readLine() again. However, if you general program requires you to read lines, you might want to consider using LineReader class, it has the same API as BufferedReader.
I want it just reads from the first line

This compiles and runs but instead of reading from the second line (where lineID1 is) it just takes the data from the first line.
 
Chris Fetterley
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to check each line to see if it has the value of lineID1. I want to know if there is a way to put all the lines into an array and check it that way.
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, the BufferedReader or LineNumberReader will be fine.

In your code, it seems that you read thru the whole file, but didn't do any real interpretatino that you wanted. You can do two things in your loop.
1. Process each line as you read them in.

2. Put each line into a Collection object, and loop thru the Collection object after the entire file is read.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!