📄 fileinputdemo.java
字号:
//FileInputDemo.java
import java.io.*;
public class FileInputDemo{
public static void main(String args[]) {
String fileName="InputFileExample.txt";
File file=new File(fileName);
FileInputStream inputStream;
//创建一个文件对象
byte[] bufferArray=new byte[10000000];
long fileLength=file.length();
System.out.println("\nLength of file " + fileName + " : " + fileLength);
try {
inputStream=new FileInputStream(file);
try {
//将文件中的内容读取到字节数组中
inputStream.read(bufferArray, 0, bufferArray.length);
System.out.println("\nFile "+fileName+" content:\n\n"
+new String(bufferArray,0,(int)fileLength));
//关闭输入文件流
inputStream.close();
}
// 捕获可能出现的输入/输出异常
catch (IOException exception) {
System.out.println("I/O Exception when reading or closing: "+exception.toString());
}
}
catch(FileNotFoundException exception){
System.out.println("File not find: "+exception.toString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -