JavaSE IO流学习总结

IO流

io流分为字节流字符流

字节流:输入流父类 InputStream 常用的子类有 FileInputStream BufferedInputStream

​ 输出流父类OutputStream 常用的子类有 FileOutputStream BfferedOutputStream

字符流:输入流父类 Reader 常用的子类有BufferedReader InputStreamReader

​ 输出流父类 Writer 常用的子类有BufferedWriter OutputStreamWriter

InputStream和OutputStream

此方法每次读写一个字节

File

拷贝文件案例

private static void copy() throws FileNotFoundException, IOException {
        FileInputStream fis = new FileInputStream("暴躁源泉.jpg");
        FileOutputStream fos = new FileOutputStream("copy.jpg");
        
        int b ;
        while((b = fis.read())!= -1) {
            fos.write(b);
        }      
        //以上三行为核心代码
        fis.close();
        fos.close();
    }

此方法相当于一次运输一个字节,效率很慢。

对于此方法进行改进

public static void main(String[] args) throws IOException {
        //copy();
        //copy2();
        FileInputStream fis = new FileInputStream("A*Teens - Upside Down.mp3");
        FileOutputStream fos = new FileOutputStream("copy2.mp3");
        
        byte[] arr = new byte[fis.available()];
        fis.read(arr);
        fos.write(arr);
        
        fis.close();
        fos.close();
        
    }

此方法相当于一次运输fis 总字节数大小的字节数,相比上一个方法效率高一点。但如果字节过多,容易造成内存崩坏。

对此方法再进行改进

    public static void main(String[] args) throws IOException {
        //demo1();
        //demo2();
        FileInputStream fis = new FileInputStream("A*Teens - Upside Down.mp3");
        FileOutputStream fos = new FileOutputStream("yyy.mp3");
        
        
        byte[] arr = new byte[1024*8];
        int len;
        while((len = fis.read(arr)) != -1) {
            fos.write(arr,0,len);
        }
        
        
        fis.close();
        fos.close();
    }

此方法将一个arr数组[1024*8]大小的内存,一次性运送这么多的字节,足够大,也不容易造成内存溢出。

BufferedInputStream


    private static void demo1() throws FileNotFoundException, IOException {
        // TODO Auto-generated method stub
        FileInputStream fis = new FileInputStream("A*Teens - Upside Down.mp3");
        FileOutputStream fos = new FileOutputStream("copy。mp3");
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream  bos = new BufferedOutputStream(fos);
        
        int b ;
        while((b=bis.read()) != -1) {
            bos.write(b);
        }
        
        bos.close();
        bis.close();
    }

需要先定义fis 对象 , 之后将fis对象传入BufferedInputStream

或者

BufferedInputStream bis =
    new BufferedInputStream(new FileInputStream("A*Teens - Upside Down.mp3"));
BufferedOutputStream bos =
    new BufferedOutputStream(new FileOutputStream("copy.mp3"));
        

Reader和Writer

字符流相比较于字节流,是以字符为单位,根据不同的编码方式(GDK,UTF-8)有不同的编译模式。

        FileReader fr = new FileReader("xxx.txt");
        int c;
        
        while((c = fr.read()) != -1) {
            System.out.print((char)c);
        }
        
        fr.close();
        FileReader fr = new FileReader("xxx.txt");
        FileWriter fw = new FileWriter("yyy.txt");
        
        char[] arr = new char[1024];
        int len;
        while((len = fr.read(arr)) != -1) {
            fw.write(arr,0,len);
        }
        
        fr.close();
        fw.close();

因为字符不可能刚好转满1024个字节,所以len的作用是判断 当前arr里有的字符的长度,空则fr.read(arr) 为-1 len 则是arr中剩下的长度。

BufferedReader

BufferedReader br = new BufferedReader(new FileReader("zzz.txt"));

新增了readline(Reader),和newline(Writer)方法。

分别为 读取当前行的字符串, 和 写出时换行。

    String line;
        while((line = br.readLine())!=null) {
            bw.write(line);
            bw.newLine();
        }
LinNumberReader
    LineNumberReader lnr = new LineNumberReader(new FileReader("zzz.txt"));
        
        String line;
        lnr.setLineNumber(100);
        while((line = lnr.readLine())!= null)
        {
            System.out.println(lnr.getLineNumber()+":"+line);
        }
        lnr.close();

lnr初始值为1,现实当前的行号。 括号里可以另外复制。

关于编码格式的更改

当在两个文件的编码格式不同的时候

BufferedReader br =
                new BufferedReader(new InputStreamReader(new FileInputStream("utf-8.txt"),"utf-8"));
        BufferedWriter bw =
                new BufferedWriter(new OutputStreamWriter(new FileOutputStream("gbk.txt"),"gbk"));
        

在定义FileInputStream后 加上当前编码格式的对应字符串。

递归查找文件的小案例


import java.io.File;
import java.util.Scanner;

public class Test5 {

    public static void main(String[] args) {
        File dir = getDir();
        printfile(dir);
    }
    
    public static File getDir() {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入一个文件夹路径");
        while(true) {
            String line = in.nextLine();
            File dir = new File(line);
            if(!dir.exists()) {
                System.out.println("您录入的文件夹路劲不存在,请重新输入");
            }else if(dir.isFile()){
                System.out.println("是文件");
            }else
            {
                return dir;
            }
        }
    }
    
    
    public static void printfile(File dir) {
        File[] subFiles =dir.listFiles();
        for (File subFile : subFiles) {
            if(subFile.isFile() && subFile.getName().endsWith(".txt")){
                System.out.println(subFile);
            }else if(subFile.isDirectory()){
                printfile(subFile);
            }
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/xijue/p/12342717.html
今日推荐