流的使用

  1 package com.ccp.Lc0813;
  2 
  3 import java.io.*;
  4 
  5 public class TextFile {
  6     public static void main(String[] args) throws IOException {
  7         File file=new File("D://text");
  8         System.out.println("——————文件——————");
  9 //        file.mkdir();
 10         File file1=new File("D://text//txt.txt");
 11         System.out.println("——————创建文件——————");
 12 //        try {
 13 //            file1.createNewFile();
 14 //        } catch (IOException e) {
 15 //            e.printStackTrace();
 16 //        }
 17         System.out.println("——————List————————");
 18         String[] list=file.list();
 19         for (String ps : list
 20         ) {
 21             System.out.println(ps);
 22         }
 23         System.out.println("——————ListFiles——————");
 24         File[] list1=file.listFiles();
 25         for (File file2 : list1
 26         ) {
 27             System.out.println(file2);//file2.getAbsolutePath();相对路径(有过程火车)file2.getPath();绝对路径(直接就到目的地瞬移)
 28         }
 29         System.out.println("——————过滤后————————");
 30         System.out.println("查找文件");
 31         FilenameFilter filenameFilter= new FileNameFile();
 32         String[] fft=file.list(filenameFilter);
 33         for (String d:fft
 34              ) {
 35             System.out.println(d);
 36         }
 37         System.out.println("——————FileFilter————————");
 38         FileFiter fileFiter=new FileFiter();
 39         File[] ft=file.listFiles(fileFiter);
 40         for (File f:ft
 41              ) {
 42             System.out.println(f);
 43         }
 44         System.out.println("———————输入流——————————");
 45         FileInputStream fi=new FileInputStream(file1);
 46         int ent=0;
 47         while ((ent=fi.read())!=-1){
 48             System.out.print((char)ent);
 49         }
 50         fi.close();
 51         System.out.println();
 52         System.out.println("——————输出流——————————");
 53         FileOutputStream fo=new FileOutputStream(file1,true);//append追加信息
 54         String str="人是铁饭是钢";
 55         fo.write(str.getBytes());
 56         fo.flush();//刷新
 57         fo.close();
 58         System.out.println("-——————字节流——————————");
 59         FileReader fr=new FileReader(file1);
 60         char[] rr=new char[1024];
 61         int enr=0;
 62         while ((enr=fr.read(rr))!=-1){
 63             System.out.println(new String(rr,0,enr));
 64         }
 65         fr.close();
 66         System.out.println("——————字符流——————————");
 67         FileWriter fw=new FileWriter(file1,true);
 68         fw.write("kkkooo");
 69         fw.flush();
 70         fw.close();
 71         System.out.println("————————传文件————————");
 72         FileInputStream f1=new FileInputStream(file1);
 73         FileOutputStream f2=new FileOutputStream(file,true);//append追加信息
 74         int ent1=0;
 75         while ((ent1=f1.read())!=-1){
 76             f2.write(ent1);
 77         }
 78         f2.flush();//刷新
 79         f2.close();
 80         f1.close();
 81 
 82         //将视频从一个位置传输到另外一个位置,使用字 节流的传输
 83 
 84         //源文件
 85         File fileold = new File("D:\\text\\ttf\\txt77.txt") ;
 86         //目标文件
 87         File filenew = new File("D:\\text\\txt.txt") ;
 88         //需要关联两个文件的管道
 89         FileInputStream fis = new FileInputStream(fileold) ;
 90         FileOutputStream fos = new FileOutputStream(filenew) ;
 91         //传输的过程,是fis读出写入fos
 92         int len = 0 ;
 93         while((len = fis.read() )!= -1){
 94             fos.write(len);
 95             System.out.print(len + " ");
 96         }
 97         //关闭通道:顺序  创建管道的时候需要创建源,再创建目标,关闭的时候,先关闭目标再关闭源
 98         fos.close();
 99         fis.close();
100         System.out.println("————————缓冲————————");
101         //将视频从一个位置传输到另外一个位置,使用字 节流的传输
102         //源文件
103         File fileold1 = new File("D:\\text\\ttf\\txt77.txt") ;
104         //目标文件
105         File filenew1 = new File("D:\\text\\ttf\\txt.txt") ;
106         //需要关联两个文件的管道
107         FileInputStream fis1 = new FileInputStream(fileold1) ;
108         FileOutputStream fos1 = new FileOutputStream(filenew1) ;
109         //将字节流转换为缓冲流
110         BufferedInputStream bis = new BufferedInputStream(fis1) ;
111         BufferedOutputStream bos = new BufferedOutputStream(fos1) ;
112         //传输的过程,是fis读出写入fos
113         int len1 = 0 ;
114         byte rate [] = new byte[1024] ;
115         while((len1 = bis.read(rate) )!= -1){
116             bos.write(rate , 0  , len1);
117             System.out.print(len1 + " ");
118         }
119         //关闭通道:顺序  创建管道的时候需要创建源,再创建目标,关闭的时候,先关闭目标再关闭源,关闭缓冲再关闭字节
120         bos.close();
121         fos.close();
122         bis.close();
123         fis.close();
124     }
125 }

猜你喜欢

转载自www.cnblogs.com/qsy0021/p/11361630.html
今日推荐