如何在Java中读取文件中的数据?
FileInputStream 类的read(byte[]b)方法从此文件输入流读取最大为b.length的字节数组。该方法将阻塞,直到有一些输入可用为止。
示例
import java.io.File;
import java.io.FileInputStream;
public class Sample {
public static void main(String args[]) throws Exception {
int count =0;
File file = new File("myData");
FileInputStream fis = new FileInputStream(file);
byte[] bytesArray = new byte[(int)file.length()];
fis.read(bytesArray);
String s = new String(bytesArray);
System.out.println("给定文件的内容为:: " +new String(bytesArray));
}
}输出结果
给定文件的内容为:: Hi how are you welcome to nhooo
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短