I need to read a file line by line using java.nio
, but nio
doesn\'t have a method like readline()
to read one, complete line at a time
NIO is typically used to do either direct memory access or block mediated bulk data transfers. It does do other things, but other features have more to do with blocking and non-blocking data access.
As such, you might want to use NIO to grab the data quickly (or in a non-blocking manner); however, if you want to "read line by line" you would be better served by doing the line detection after NIO has read in the available data. This could easily be implemented by putting a "line reading" facade over the buffer that NIO just read.
Oracle introduces a example in the tutorial. https://docs.oracle.com/javase/tutorial/essential/io/file.html#streams
Path file = ...;
try (InputStream in = Files.newInputStream(file);
BufferedReader reader =
new BufferedReader(new InputStreamReader(in))) {
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException x) {
System.err.println(x);
}
It works for me....
public static void main(String[] args) throws IOException
{
RandomAccessFile aFile = new RandomAccessFile
("F:\\DetailsMy1.txt", "r");
FileChannel inChannel = aFile.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1);
StringBuffer line = new StringBuffer();
while(inChannel.read(buffer) > 0)
{
buffer.flip();
for (int i = 0; i < buffer.limit(); i++)
{
char ch = ((char) buffer.get());
if(ch=='\r'){
System.out.print(line+"[EOL]");
line=new StringBuffer();
}else{
line.append(ch);
}
}
buffer.clear(); // do something with the data and clear/compact it.
}
inChannel.close();
aFile.close();
}
I understand you guys don't like limitations, but in case the one asking don't have access to IO package or not allowed to import it for some reason the top answer is not helpful...
Two ways to do it completely IO free:
java.nio.file.Files.lines
,
Returns a stream of lines, which is part of .util package and not .io package like bufferedReader.
java.nio.file.Files.readAllLines
,
Returns a collection of lines which is iterable.
Proceed to use an iterator
orfor each
to extract a single line.
cheers
Using java.nio.file.Files you can do:
Path path = FileSystems.getDefault().getPath("/path/to", "file.txt");
Files.lines(path).forEach(line ->
// consume line
);
As the lines(path)
method returns a Stream, you can take advantage of any other method of the Stream API, like reading just the first line (if one exists) with:
Optional<String> firstLine = Files.lines(path).findFirst();
simply use: https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#lines-java.nio.file.Path-
EDIT (humanreadable translation, regards to MeetTitan): Which means: use java.nio.file.Files.lines(Path)
- it returns a Stream<String>
representing the lines of the file. It's an method provided by the Java API. One could consult the Javadoc in order to see the details. The most relevant information is therefore: Files.lines()
exists since Java 1.8. Use it.