7-23 IO流

PrintStream

使System.out.println(“skldsk”);读入一个文件中而不是显示在屏幕中

File  file = new File("E:\\11.txt");        
      PrintStream ps =null;
    try {
        ps = new  PrintStream(file);
        System.setOut(ps);
        System.out.println("hello world");
    } catch (FileNotFoundException e) {

        e.printStackTrace();
    }

BufferedWriter

try {     
            FileOutputStream is =  new  FileOutputStream(file);
        OutputStreamWriter reader = new OutputStreamWriter(is);
            Reader reader = new Reader(is);
            BufferedWriter    br = new BufferedWriter(reader);
            br.write("你好");
        br.newLine();
        br.write("你好");
            br.flush();
            is.flush();
            reader.flush();
            is.close();
            reader.close();
            br.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

        //copy( "e:\\11.txt","e:\\22.txt");
        //ergodiFiles("e:");

FileOutputStream

Scanner scanner = new Scanner(System.in);
        String s = scanner.next();
        File file = new File("E:\\22.txt");
        if(!file.exists()){
                try {
                    file.createNewFile();
                } catch (IOException e) {

                    e.printStackTrace();
                }

        }
        FileOutputStream  out =null;
        try{
             out = new FileOutputStream(file);
            out.write(s.getBytes());
            out.flush(); //
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                out.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }

用Filter去过滤文件

ZipfilenameFilter filter = new ZipfilenameFilter();
        File file = new File("e:\\");
        File[] files = file.listFiles(filter);
        for(int i =0;i<files.length;i++){
            System.out.println(files[i].getAbsolutePath());
        }
import java.io.File;
import java.io.FileFilter;

public class ZipfilenameFilter implements FileFilter{

    //Filter
    public boolean accept(File  f) {

        return f.getName().endsWith("mp4");

    }


}
        }

file寻找MP4格式的文件。

public static void ergodiFiles(String filepath){
        File file = new File(filepath);
        File[] files = file.listFiles();//返回此文件中的所有文件路径。
        for(int i =0;i<files.length;i++){
            if(files[i].isDirectory()){//判断是否是文件夹
                //System.out.println(files[i]);
                ergodiFiles(files[i].getAbsolutePath());
            }else{
                if(files[i].getName().endsWith("mp4")){
                    System.out.println(files[i].getAbsolutePath());//获得文件的路径
                }
            }
        }
    }

将文件从一个文件copy到另一个文件

public static void copy(String sou,String copy){
        File source = new File("sou");
        File  file  =new File("copy");
        try {
            FileInputStream  sourceIn = new FileInputStream(source);
            FileOutputStream  fileOut  = new FileOutputStream(file);

            byte[] array = new byte[1024];
            int i =0;
            while(i!=-1){
                i = sourceIn.read(array);
                if(i!=-1){
                    fileOut.write(array, 0, i);
                }


            }
            sourceIn.close();
            fileOut.flush();
            fileOut.close();

        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

你可能感兴趣的:(7-23 IO流)