关于文件输入流

构造方法摘要

方法摘要

示例代码
1、读取文件内容
import java.io.FileInputStream;import java.io.IOException;public class Main { public static void main(String[] args) {// readFile01(); readFile02(); } /** * 一次读取一个字节,效率比较低 */ public static void readFile01() { String filePath = "./hello.txt"; int data = 0; FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(filePath); while ((data = fileInputStream.read()) != -1) { System.out.print((char) data); } } catch (IOException e) { e.printStackTrace(); } finally { // 关闭文件流,释放资源。。,。,。. try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 一次读取 8 个字节 */ public static void readFile02() { String filePath = "./hello.txt"; int readLen = 0; byte[] buf = new byte[8]; FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(filePath); while ((readLen = fileInputStream.read(buf)) != -1) { System.out.print(new String(buf, 0, readLen)); } } catch (IOException e) { e.printStackTrace(); } finally { // 关闭文件流,释放资源。。,。,。. try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }}