File tempFile = new File(loadedFileName);
FileInputStream datStream = new FileInputStream(tempFile);
InputStreamReader readDat = new InputStreamReader(datStream);
int data = readDat.read();
String temp = "";
// keeps reading in one character at a time and returns -1 if there are no more
// characters
while(data != -1){
char datChar = (char)data;
if(temp.length() > 2){
if((temp.substring(temp.length()-1)).equals("\n")){
String[] arrayTemp = temp.split("\\|");
if(Float.valueOf(arrayTemp[columnNumber-1]) > value){
System.out.print(temp);
}
temp = "";
}
}
temp = temp+datChar;
data = readDat.read();
}
The code reads in a file character by character and appends it to a string. Once it reaches a new line it will split the string into an array and then checks to see if a value in that array matches and prints out the string before it was split.
The problem with this code is that even though it gets most of the job done, because of how it is in a while loop where it checks to see if it reaches the end, which if it does it returns -1. This makes it so that I can't print the last line of the file because there is no new line at the end of the file, so it kills the loop before it gets to print out the last line.
An example of a few lines being read in.
This | world | is | brown | and | dirty|
24 | hours | are | in | day| Hello|
Can't store the whole file into an array, can't use a buffered reader, I've tried doing something like counting the number of "|" but I can't seem to get that to work. So in this case if it counted to 6 pipes it would then split and then check before printing. Which I think would solve the problem of not printing the last line. Here is how I tried to implement the count for the |.
while(data != -1){
char datChar = (char)data;
// checks to make sure that temp isn't an empty string first
if(temp.length() > 2){
// checks to see if a new line started and if it did splits the current string into an array.
if((temp.substring(temp.length()-1)).equals("\\|")){
if(count == 6){
String[] arrayTemp = temp.split("\\|");
//then checks the variable in the array col and compares it with a value and prints if it is greater.
if(Float.valueOf(arrayTemp[columnNumber-1]) > value){
System.out.print(temp);
}
temp = "";
count = 0;
}
}
}
temp = temp+datChar;
data = readDat.read();
}
You'd be better off using a BufferedReader
and readLine
. That will be more efficient in terms of IO, and means you don't need to worry about handling the line breaks yourself:
BufferedReader reader = new BufferedReader(readDat);
String line;
while ((line = reader.readLine()) != null) {
// Handle a line of data - check for it being empty, split it etc.
}
This will give you all the lines in the file, whether or not there's a terminating newline.
EDIT: If you really can't use BufferedReader
, I would make your code significantly simpler by checking whether the current character is \n
or the end of the file, and processing the "line so far" if so:
StringBuilder line = new StringBuilder();
while (true) {
int next = readDat.read();
if (next == '\n' || next == -1) {
// Handle line here
line = new StringBuilder();
if (next == -1) {
break;
}
} else {
line.append((char) next);
}
}
Note the use of StringBuilder
instead of repeated concatenation.
See more on this question at Stackoverflow