Java中IO流读取在项目中的文件中的数据

首先需要获取项目中文件的地址–>右键点击 项目–>properties–>Location 就是想本项目中的文件的地址
创建文件字符流 ,定义一个字符数组,用于存储读到字符
循环读取 最后用new String(b,0,m) 转成字符串赋值给定义的字符串

File file = new File("D:\\Eclipse-Work-javaSR\\week3\\test.txt");
FileReader fr = new FileReader(file);
  int m=0;
  char[] b = new char[1024];
  String line="";
while((m=fr.read(b))!=-1){
   line=new String(b,0,m);
  }
System.out.println(line); 

这样就可以读到了

你可能感兴趣的:(Java中IO流读取在项目中的文件中的数据)