import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FileTest {
public static void main(String[] args) {
File file=new File("E://wwwroot/index.html");
if(file.exists()){
// 以字节流方法读取文件
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
// 设置一个,每次 装载信息的容器
byte[] buf = new byte[1024];
// 定义一个StringBuffer用来存放字符串
StringBuffer sb = new StringBuffer();
// 开始读取数据
int len = 0;// 每次读取到的数据的长度
while ((len = fis.read(buf)) != -1) {// len值为-1时,表示没有数据了
// append方法往sb对象里面添加数据
sb.append(new String(buf, 0, len, "utf-8"));
}
// 输出字符串
System.out.println(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
JAVA语言IO读取文件内容
最新推荐文章于 2022-08-18 13:49:36 发布