Reputation: 589
I'm having problems with using the BufferedReader
I want to print the 6 lines of a text file:
public class Reader {
public static void main(String[]args) throws IOException{
FileReader in = new FileReader("C:/test.txt");
BufferedReader br = new BufferedReader(in);
while (br.readLine() != null) {
System.out.println(br.readLine());
}
in.close();
}
Now from what I can gather every time I call the readLine() method it automatically advances to the next line.
So I can't use the condition br.readLine() != null since it'll already advance it one line and I get the output:
Line 2
Line 4
Line 6
What Condition do I use to check if there is still a new line in the text field?
Upvotes: 35
Views: 188422
Reputation: 812
Use try with resources. this will automatically close the resources.
try (BufferedReader br = new BufferedReader(new FileReader("C:/test.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
}
Upvotes: 2
Reputation: 131
private void readFile() throws Exception {
AsynchronousFileChannel input=AsynchronousFileChannel.open(Paths.get("E:/dicom_server_storage/abc.txt"),StandardOpenOption.READ);
ByteBuffer buffer=ByteBuffer.allocate(1024);
input.read(buffer,0,null,new CompletionHandler<Integer,Void>(){
@Override public void completed( Integer result, Void attachment){
System.out.println("Done reading the file.");
}
@Override public void failed( Throwable exc, Void attachment){
System.err.println("An error occured:" + exc.getMessage());
}
}
);
System.out.println("This thread keeps on running");
Thread.sleep(100);
}
Upvotes: 1
Reputation: 1703
you can store it in array and then use whichever line you want.. this is the code snippet that i have used to read line from file and store it in a string array, hope this will be useful for you :)
public class user {
public static void main(String x[]) throws IOException{
BufferedReader b=new BufferedReader(new FileReader("<path to file>"));
String[] user=new String[30];
String line="";
while ((line = b.readLine()) != null) {
user[i]=line;
System.out.println(user[1]);
i++;
}
}
}
Upvotes: 0
Reputation: 364
or
public String getFileStream(final String inputFile) {
String result = "";
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader(inputFile)));
while (s.hasNext()) {
result = result + s.nextLine();
}
} catch (final IOException ex) {
ex.printStackTrace();
} finally {
if (s != null) {
s.close();
}
}
return result;
}
This gets first line as well.
Upvotes: 0
Reputation: 1129
Try:
String text= br.readLine();
while (text != null)
{
System.out.println(text);
text=br.readLine();
}
in.close();
Upvotes: 0
Reputation: 5638
You read line
through while
loop and through the loop you read the next line ,so just read it in while loop
String s;
while ((s=br.readLine()) != null) {
System.out.println(s);
}
Upvotes: 6
Reputation: 4307
Maybe you mean this:
public class Reader {
public static void main(String[]args) throws IOException{
FileReader in = new FileReader("C:/test.txt");
BufferedReader br = new BufferedReader(in);
String line = br.readLine();
while (line!=null) {
System.out.println(line);
line = br.readLine();
}
in.close();
}
Upvotes: 0
Reputation: 9705
You can assign the result of br.readLine()
to a variable and use that both for processing and for checking, like so:
String line = br.readLine();
while (line != null) { // You might also want to check for empty?
System.out.println(line);
line = br.readLine();
}
Upvotes: 5
Reputation: 1500515
This is the problem:
while (br.readLine() != null) {
System.out.println(br.readLine());
}
You've got two calls to readLine
- the first only checks that there's a line (but reads it and throws it away) and the second reads the next line. You want:
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
Now we're only calling readLine()
once per loop iteration, and using the line that we've read both for the "have we finished?" and "print out the line" parts.
Upvotes: 80