package java7; import org.junit.Test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; /** * 测试FileInputStream和FileOutputStream * * @author 冀帅 * @date 2020/8/19-15:49 * 结论:对于文本文件(.txt,.java.c.cpp),使用字符流处理 * 对于非文本文件(.jpg..mp3.mp4.avi.doc.ppt),使用字节流输出流、 * * */ public class FileInOUt { @Test public void test() { // 1.创建File类对象 FileInputStream in = null; try { File file = new File("hello.txt"); // 2.常见流对象 in = new FileInputStream(file); // 3.操作数据 byte[] Bity = new byte[1024]; int len ;//记录每次读取字节的个数 while ((len = in.read(Bity))!=-1){ // 方式一: String str = new String(Bity,0,len); System.out.print(str); // } } catch (IOException e) { e.printStackTrace(); } finally { try { if (in!=null) in.close(); } catch (IOException e) { e.printStackTrace(); } } } }