IO流之流
流的分类
按照流向分为:输入流和输出流。
按照处理单位分为:字节流和字符流
按照功能为:节点流和转换流。
InputStream(字节输入流) / OutputStream(字节输出流)
InputStream是所有字节输入流的抽象父类,提供了
read():从数据源中读取一个字节 (返回值为读取的字节,输出需要转换为字符型。若读取超过字节的长度返回-1)
read(byte [ ] buf):读取一定数量的字节到缓冲数组buf中
OutputStream是所有字节输出流的抽象父类,提供了
write():写入一个字节到数据源
write(byte [] buf):写入一定量的字节到缓冲数组buf中
FileInputStream 文件字节输入流:专门用于从文件中读取字节到程序内存中。即为流通道
FileOutputStream 文件字节输出流:专门用于从程序内存写入字节到文件中。即为流通道
读取一个字节:
1 public static void main(String[] args) { 2 3 // 需求:读取一个文件中的一个字节 4 File file = new File("d:\\sxt2019\\a.txt"); 5 6 try { 7 //1.创建通道 8 FileInputStream in=new FileInputStream(file); 9 10 int t; 11 StringBuilder sb=new StringBuilder(); 12 try { 13 //2.读取单个字节,输出时要转换为char类型 14 t=in.read(); 15 t=in.read(); 16 t=in.read(); 17 t=in.read(); 18 System.out.println((char)t); 19 20 21 //3.关闭通道 22 in.close(); 23 } catch (IOException e) { 24 // TODO Auto-generated catch block 25 e.printStackTrace(); 26 } 27 } catch (FileNotFoundException e) { 28 // TODO Auto-generated catch block 29 e.printStackTrace(); 30 } 31 32 }
读取多个字节:
1 ublic static void main(String[] args) throws FileNotFoundException { 2 File file = new File("d:\\sxt2019\\a.txt"); 3 4 //1.创建通道 5 FileInputStream fileInputStream=new FileInputStream(file); 6 7 //2.创建字节数组 8 byte[] buf=new byte[5]; 9 10 int len; 11 12 StringBuilder sb=new StringBuilder(); 13 //3.循环读取字节到字节数组中 14 try { 15 while((len=fileInputStream.read(buf))!=-1){ 16 17 //4.字节转换为字符串 18 String string=new String(buf, 0, len); 19 sb.append(string); 20 21 22 } 23 System.out.println(sb); 24 } catch (IOException e) { 25 // TODO Auto-generated catch block 26 e.printStackTrace(); 27 } 28 }
按照指定编码写入文件中:
1 public static void main(String[] args) throws IOException { 2 File file = new File("d:\\sxt2019\\b.txt"); 3 4 //1.创建流通道(文件字节输出流) 5 FileOutputStream fileOutputStream=new FileOutputStream(file); 6 7 //2.要写入的字符串 8 String str="i love you"; 9 //3.字符串按规定编码转换为字节数组 10 byte[] b=str.getBytes("UTF-8"); 11 //4.通过通道写入文件 12 fileOutputStream.write(b); 13 //5.关闭通道 14 fileOutputStream.close(); 15 }
从指定路径复制图片到工程目录中:
1 public static void main(String[] args) throws IOException { 2 File orFile=new File("d:\\sxt2019\\hzu.jpg"); 3 File toFile=new File("hzu.jpg"); 4 5 long fileSize=orFile.length(); 6 long cpysize=0; 7 float progress=0.0f; 8 9 FileInputStream fileInputStream=new FileInputStream(orFile); 10 FileOutputStream fileOutputStream=new FileOutputStream(toFile); 11 12 byte[] buf=new byte[512]; 13 14 int len; 15 while((len=fileInputStream.read(buf))!=-1){ 16 fileOutputStream.write(buf, 0, len); 17 18 cpysize+=len; 19 progress=cpysize*1.0f/fileSize; 20 System.out.println(progress); 21 } 22 fileInputStream.close(); 23 fileOutputStream.close(); 24 System.out.println("复制完成!"); 25 26 }
Reader(字符输入流) / Writer(字符输出流)
Reader是字符输入流的抽象父类,提供了
read():一次读取一个字符
read(char[] cbuf):一次读取多个字符到字符缓冲区cbuf中,返回的值为读取的字符个数。
Writer是字符输出流的抽象父类,提供了
write():一次写入一个字符
write(char[] cbuf):一次写入多个字符到字符缓冲区cbuf中,返回的值为写入的字符个数。
write(String):写入字符串
FileReader 文件字符输入流 ,专门用于读取默认字符编码(GBK)文本文件。
FileWriter 文件字符输出流 ,专门用于写入默认字符编码(GBK)文本文件。注意:为了提高效率,FileWriter内部存在一个字节缓冲区,用于对待写入的字符进行统一编码到字节缓冲区,一定要在关闭流之前,调用flush方法刷新缓冲区。
读取一个或多个字符到字符缓冲区cbuf:
1 public static void main(String[] args) throws IOException { 2 3 File file = new File("d:\\sxt2019\\a.txt"); 4 5 FileReader reader = new FileReader(file); 6 7 // 【1】一次读取一个字符 8 9 /*int c; 10 c = reader.read(); 11 c = reader.read(); 12 c = reader.read(); 13 c = reader.read(); 14 c = reader.read(); 15 System.out.println((char)c); 16 */ 17 char[] cbuf=new char[2]; 18 int len; 19 StringBuilder sb=new StringBuilder(); 20 while ((len=reader.read(cbuf))!=-1){ 21 sb.append(cbuf, 0, len); 22 23 } 24 System.out.println(sb); 25 26 reader.close(); 27 }
写入字符到文件中:
1 public static void main(String[] args) throws IOException { 2 File file = new File("d:\\sxt2019\\b.txt"); 3 4 FileWriter writer=new FileWriter(file); 5 6 //写入一个字符 7 /*writer.write('9');*/ 8 9 10 //写入多个字符 11 /*char[] cbuf={'i','l','o','v','e',' ','y'}; 12 writer.write(cbuf);*/ 13 14 //写入一个字符串 15 String string="i like you"; 16 writer.write(string); 17 18 19 writer.flush(); 20 writer.close(); 21 22 }