I have a text file. I want to read it line by line and turn it into an 2-dimensional array. I have written something as follows:
BufferedReader br = new BufferedReader (new FileReader ("num.txt"));
String line = br.readLine();
while( line != null) {
System.out.printf(line);
}
This turns into an infinite loop. I want to move on to the next line after I'm done with reading and printing a line. But I don't know how to do that.
You only read the first line. The line variable didn't change in the while loop, leading to the infinite loop.
Read the next line in the while condition, so each iteration reads a line, changing the variable.
BufferedReader br = new BufferedReader (new FileReader ("num.txt"));
String line;
while( (line = br.readLine() ) != null) {
System.out.printf(line);
}
BufferedReader br = new BufferedReader (new FileReader ("num.txt"));
String line = br.readLine();
while( line != null) {
System.out.printf(line);
// read the next line
line = br.readLine();
}
... or read the line in the while condition (as rgettman pointed out):
String line;
while( (line = br.readLine()) != null) {
System.out.printf(line);
}
Related
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 am writing code that
first) asks the user for a file name
second) reads the file and puts each line into an ArrayList
third) prints out the ArrayList
My code is reading the file with a BufferedReader, but it is only printing out the first line 25 times instead of printing out the 25 different lines.
This is what my while loop looks like. I don't know how to increment it though
ArrayList<String> stringArray = new ArrayList<String>();
BufferedReader reader = null;
reader = new BufferedReader(new FileReader(fileName));
String line = reader.readLine();
while(reader.readLine() != null){
stringArray.add(line);
}
return stringArray;
Any thoughts?
You are not reading in the line to the variable on each run, you need to read it in the while loop.
String line = reader.readLine();
while(line != null){
stringArray.add(line);
line = reader.readLine(); // read the next line
}
return stringArray;
This is not the preferred solution. Just showing that it can be done a different way
Or you can use a do...while instead of while...do.
String line;
do {
line = reader.readLine();
if (line != null) {
stringArray.add(line);
}
} while (line != null);
You can see why this isn't the preferred solution. You are doing 2 null checks where you can get away with 1.
while (true) {
String line = reader.readLine();
if (line == null) break;
stringArray.add(line);
}
return stringArray;
I am trying to read some lines from a file in Java. I have 4 lines in the file, but the problem is that it reads only 2 of the lines. Here's the code:
BufferedReader flux_in = new BufferedReader(new InputStreamReader(new FileInputStream("abc.txt")));
String line;
while (flux_in.readLine() != null)
{
line = flux_in.readLine();
System.out.println(line);
}
It is because you are calling readLine twice as often as you should.
Your first call inside the the while condition just threw the line away.
BufferedReader flux_in = new BufferedReader(new InputStreamReader(new FileInputStream("abc.txt")));
String line;
while ((line = flux_in.readLine()) != null)
{
System.out.println(line);
}
It does read all of them, although not quite in the way you'd like.
BufferedReader flux_in = new BufferedReader(new InputStreamReader(new FileInputStream("abc.txt")));
String line;
while (flux_in.readLine()!=null) //one line is read here
{
line = flux_in.readLine(); //the next one here
System.out.println(line);
}
In your code, the call in the loop test consumes a line, and then the call n the loop body consumes another line. So it would only print every other line.
String s = null;
while ((s = flux_in.readLine()) != null)
{
System.out.println(s);
}
BufferedReader is stateful and remembers what has been read from the file already. Every call of readLine() moves the cursor to the next line. You are calling readLine() twice per line: in the while loop and in the line assignment. Try this instead:
String line = flux_in.readLine();
while (line != null) {
System.out.println(line);
line = flux_in.readLine();
}
You're reading the line twice (in the while condition and in the while loop, so it's displaying one and then skipping the next. Change your code to this:
try {
BufferedReader flux_in = new BufferedReader(new FileReader("abc.txt"));
String line;
while ((line = flux_in.readLine()) != null)
System.out.println(line);
} catch(Exception e) { System.out.println("Error"); }
I am a newbie to java. I am reading a big file using BufferedReader. How can I know the offset of a line so that I can store it in some other file for indexing purposes?
EDIT:
Here's my code
path=FileSystems.getDefault().getPath(".",filename);
br=Files.newBufferedReader(path_doc_title_index_path, Charset.defaultCharset());
int offset=0; //offset of first line.
String strline=br.readline();
offset+=strline.length()+1; //offset of second line
If you are asking which line you are reading in a file, a simple line counter would work
BufferedReader br = new BufferedReader(new FileReader("C:\\testing.txt"));
int lineNum = 0;
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
lineNum++;
}
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