public class Method {
// 写一个方法用来调用数据
public static void data(File file){
// File file = new File(“D:/data.txt”);
// 判断文件是否存在
if (file.exists()) {
System.out.println(“文件存在!”);
// 创建fileinputstream
try { FileInputStream in = new FileInputStream(file);
// 创建byte数组
byte byt[] = new byte[1024];
// 从文件中读取信息
int len = in.read(byt); System.out.println(“数据为:” + new String(byt, 0, len));
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace(); } catch (IOException e) {
e.printStackTrace(); }
} else {
System.out.println(“文件不存在!”);
} }
public static void main(String[] args) {
// 调用数据 data(new File(“D:/data.txt”));
}}
java io流 如何读取文件中的数据
最新推荐文章于 2025-03-09 21:07:45 发布