Java基础之读文件——从文件中读取文本(ReadAString)

时间:2021-10-07 16:23:49

控制台程序,使用通道从缓冲区获取数据,读取Java基础之写文件(BufferStateTrace)写入的charData.txt

 import java.nio.file.*;
import java.nio.channels.ReadableByteChannel;
import java.io.IOException;
import java.nio.ByteBuffer; public class ReadAString { public static void main(String[] args) { Path file = Paths.get(System.getProperty("user.home")).resolve("Beginning Java Struff").resolve("charData.txt");
if(!Files.exists(file)) {
System.out.println(file + " does not exist. Terminating program.");
System.exit(1);;
} ByteBuffer buf = ByteBuffer.allocate(50);
try (ReadableByteChannel inCh = Files.newByteChannel(file)){
while(inCh.read(buf) != -1) {
System.out.print("String read: " +
((ByteBuffer)(buf.flip())).asCharBuffer().toString());
buf.clear(); // Clear the buffer for the next read
}
System.out.println("EOF reached.");
} catch(IOException e) {
e.printStackTrace();
}
}
}