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[
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);
}
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);
}
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.