This question already has answers here:
Using BufferedReader.readLine() in a while loop properly
(7 answers)
Closed 7 years ago.
if you have something like this
FileReader fileReader =
new FileReader(fileName);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
Why does bufferedeader.readline() read the next line after the first one? What's confusing to me is that there isn't a readnextline method and I don't understand why readline would continue reading the rest of the file instead of looping the first line infinitely.
You can rewrite this to:
line = bufferedReader.readLine()
while (line != null) {
... print ...
line = bufferedReader.readLine();
That should answer you question ...
(the point being the fact that readLine(); well, reads ONE line; after the other; and returns null if there wasnt any more line to read)
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I am tring to read a file which has lines like so:
438782 Abaca bunchy top virus NC_010314 Musa Sp.
So the lines contain information seperated by tabs. I am tring to read this file and do something with every line after splitting them. It keeps throwing a NullPointerException error though. This always happens on the line where I try to split. In the code below I left everything unrelated to this issue out.
BufferedReader br = new BufferedReader(new FileReader(filename));
String nextLine = br.readLine();
String[] line;
while (nextLine != null) {
nextLine = br.readLine();
line = nextLine.split("\t"); //Error line
//Do something with line
}
This line should be the last one inside your while loop, not the first
nextLine = br.readLine();
This question already has answers here:
How to read a specific line using the specific line number from a file in Java?
(19 answers)
Closed 5 years ago.
My program is reading all lines in the file but i just need the second one.
String line;
try (
InputStream fis = new FileInputStream(source);
InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
BufferedReader br = new BufferedReader(isr)) {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
If you only need the second line and you are sure the file always has at least two lines, you can just read twice and ignore the first time.
br.readLine(); //read, but ignore
System.out.println(br.readLine()); // read and output
This question already has answers here:
Reading a plain text file in Java
(31 answers)
Closed 5 years ago.
I have text file which i have read once user upload, how can i read using java code, can any one give some suggestion which would very helpful to me
sample file looks like below
SNR Name
1 AAR
2 BAT
3 VWE
A common pattern is to use
try (BufferedReader br = new BufferedReader(new FileReader(file)) ) {
String line;
while ((line = br.readLine()) != null) {
// process the line to insert in database.
}}
The easiest way is use Scanner() object
Scanner sc = new Scanner(new File("myFile.txt"));
use
boolean hasNext = sc.hasNext();
to know if there are more items in the file
and
String item = sc.next();
to get items secuentially.
I attach the documentation (it provides very good code examples)
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
You can use BufferedReader to read file line by line.
So, even if your file is too big, then also it will read line by line only. It won't load entire file. Declaration will be similar to this.
BufferedReader br = new BufferedReader(new FileReader(FILENAME));
And
you can use command
while ((sCurrentLine = br.readLine()) != null) { .....// process
}
to read file till end.
Obviously, you have to use seperator in file to split the records in each line. And store accordingly in java objects.
After that you can store in DB through DAO.
i have to count the lines on a file but later in the code I also have to print what's in that file, but I can't use the reader twice it just says null. How can I work this out without creating a bunch of bufferedreader objects?
thanks
Print and count at the same time?
Move the lines to an array then print them?
Make sure you've closed the file before reopening again?
Try closing the buffer, and then re-opening it again.
BufferedReader bufferedReader = new BufferedReader(new FileReader("src/main/java/com/william/sandbox/stackoverflow/samples20160306/Demo.java"));
String line = bufferedReader.readLine();
int lineCount = 0;
while(line != null){
lineCount += 1;
line = bufferedReader.readLine();
}
System.out.println("Line count is: " + lineCount);
bufferedReader.close();
bufferedReader = new BufferedReader(new FileReader("src/main/java/com/william/sandbox/stackoverflow/samples20160306/Demo.java"));
line = bufferedReader.readLine();
while(line != null){
System.out.println(line);
line = bufferedReader.readLine();
}
}
You can use BufferedReader's mark() and reset() methods to jump back to a specific position.
try (BufferedReader r = new BufferedReader(new FileReader("somefile.txt"))) {
// marks this position for the next 10 characters read
// after that the mark is lost
r.mark(10);
// do some reading
// jump back to the mark
r.reset();
}
Note that, BufferedReader supports marking but not all Readers do. You can use markSupported() to check.
I'm trying to read a csv file from my java code. using the following piece of code:
public void readFile() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
lines = new ArrayList<String>();
String newLine;
while ((newLine = br.readLine()) != null) {
newLine = br.readLine();
System.out.println(newLine);
lines.add(newLine);
}
br.close();
}
The output I get from the above piece of code is every alternative line [2nd, 4th, 6th lines] is read and returned by the readLine() method. I'm not sure why this behavior exists. Please correct me if I am missing something while reading the csv file.
The first time you're reading the line without processing it in the while loop, then you're reading it again but this time you're processing it. readLine() method reads a line and displaces the reader-pointer to the next line in the file. Hence, every time you use this method, the pointer will be incremented by one pointing to the next line.
This:
while ((newLine = br.readLine()) != null) {
newLine = br.readLine();
System.out.println(newLine);
lines.add(newLine);
}
Should be changed to this:
while ((newLine = br.readLine()) != null) {
System.out.println(newLine);
lines.add(newLine);
}
Hence reading a line and processing it, without reading another line and then processing.
You need to remove the first line in a loop body
newLine = br.readLine();
In java 8, we can easily achieve it
InputStream is = new ByteArrayInputStream(byteArr);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
List<List<String>> dataList = br.lines()
.map(k -> Arrays.asList(k.split(",")))
.collect(Collectors.toCollection(LinkedList::new));
outer list will have rows and inner list will have corresponding column values