本文连接:https://blog.csdn.net/infant09/article/details/80044868
首发连接:https://www.jianshu.com/p/a23ca155f949
本文作者:gks09@qq.com
仓促成文,还请指正。
FileInputStream典型代码
public static void main(String[] args) {
System.out.println(System.getProperty("user.dir"));
File file = new File(System.getProperty("user.dir") + "/src/oio/file.txt");
System.out.println("file name: " + file.getName());
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()];
int len = inputStream.read(bytes);
System.out.println("bytes len :" + len + " detail: " + new String(bytes));
} catch (IOException e) {
e.printStackTrace();
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
FileChannel典型代码
public class NIOTest {
public static void main(String[] args) throws IOException {
ByteBuffer byteBuffer = ByteBuffer.allocate(4);//①
Path path = Paths.get(System.getProperty("user.dir") + "/assets/file.txt");
FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.READ);//②
int len = fileC