字节流
i/o流的概述和分类
输入:就是将内容加载到内存中 读数据 input
输出:将内存的内容输出到硬盘中 写数据 output 将数据写入文件
i/o流按照数据类型来分类
字节流 字符流
字节流写数据
import java.io.FileInputStream; import java.io.IOException; //字节一次读一个字节数据 public class F5 { public static void main(String[] args) throws IOException { FileInputStream f = new FileInputStream("D:\\javaww\\java.txt"); //代表什么 /*//第一次读取数据 int len = f.read(b); //len代表的是什么 System.out.println(len); System.out.println(new String(b)); System.out.println("================"); //第二次读取数据 len = f.read(b); System.out.println(len); System.out.println(new String(b,0,len)); System.out.println("================"); //第三次读取数据 len = f.read(b); System.out.println(len); System.out.println(new String(b,0,len)); */ byte[] b = new byte[1024]; int len; while((len= f.read(b)) != -1){ System.out.print(new String(b)); } //释放资源 f.close(); } }
字节缓冲流
import java.io.*; //字节缓存流 public class F7 { public static void main(String[] args) throws IOException { /*BufferedOutputStream b = new BufferedOutputStream(new FileOutputStream("D:\\javaww\\java.txt")); b.write("hello\r\n".getBytes()); b.write("world\r\n".getBytes()); */ BufferedInputStream b = new BufferedInputStream(new FileInputStream("D:\\javaww\\java.txt")); int by; while((by = b.read()) !=-1 ){ System.out.print((char)by); } b.close(); } }
2021-03-18 20:25:20
浙公网安备 33010602011771号