一.针对文件内容的读取,在平时的工作中想必是避免不了的操作,现在我将自己如何用java方法读取文件中内容总结如下:废话不多说,直接上代码:
1 public static void main(String[] args) throws IOException { 2 FileInputStream fileInputStream = null; 3 try { 4 // 1.获取文件指定的文件信息 5 fileInputStream = new FileInputStream("D:\\softwore\\workspace\\springbootdemo\\node10-boot-mybatis\\src\\main\\resources\\test.txt"); 6 // 2.将数据读到字节数组里 7 byte[] buff = new byte[1024]; 8 int length = fileInputStream.read(buff); 9 // 3.将字节数据转换为字符串 10 // 参数一:带转换的字节数组,参数二:起始位置 参数三:转换的长度 11 String info = new String(buff, 0, length); 12 System.out.println(info); 13 } catch (IOException e) { 14 e.printStackTrace(); 15 } finally { 16 // 4,关闭流操作 17 if (fileInputStream != null) 18 fileInputStream.close(); 19 }
二.执行结果如下
1.文件存放位置
2.结果如下: