javaSE高级开发----文件I/O应用

打印出指定目录下的所有文件,并按级排列

public class FileTest {
    public static void main(String[] args) {
        File file = new File("D:" + File.separator + "TL-BITE" + File.separator + "Test");
        Path pa = Paths.get("D:", "TL-BITE", "MySQL");

        listAllFile(pa.toFile(), 0);
    }

    public static void listAllFile(File file, int level) {
        if (file.isFile()) {
            System.out.println(fileLeval(level) + "|---" + file.getName());
        } else {
            System.out.println(fileLeval(level)+"+" + file.getName());
//利用File类的listFile方法将所有文件列出
            File[] files = file.listFiles();
            level++;
            //利用for-each循环,判断每个列出的文件
            for (File f : files) {
            //若是目录,等级+1递归调用此方法打印输出文件名
                if (f.isDirectory()) {
                   level++;
                    listAllFile(f, level);
                }else{
                //若是文件,直接递归调用此方法,打印输出文件名
                    listAllFile(f, level);
                }

            }

        }
    }

    public static String fileLeval(int count) {
        StringBuffer space = new StringBuffer(" ");
        for (int i = 0; i < count; i++) {
            space.append(" ");
        }
        return space.toString();
    }

自定义输出流

public class PrintTest {
    private OutputStream outputStream;

    public PrintTest(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public void print(String string) {
        try {
        //将指定的字节数组写入此输出流
            outputStream.write(string.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void println(String string) {
        try {
         //将指定的字节数组写入此输出流并换行
            outputStream.write(string.getBytes());
            outputStream.write('\n');
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void println(int in) {
        println(String.valueOf(in));
    }

    public void println(double d) {
        println(String.valueOf(d));
    }

    public static void main(String[] args) {
        //传入参数为标准输出即控制台,意味着将打印输出写入控制台
        //用途与Sysout.out类似
            PrintTest pt=new PrintTest(System.out);
        
        try (FileOutputStream fos = new FileOutputStream("D:\\TL-BITE\\Test\\print.txt");
        ) {
            //参数为文件,意味着将自定义打印输出流写入文件
       //     PrintTest pt = new PrintTest(fos);
            pt.println("你好");
            pt.println(12);
            pt.println(12.45);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

利用内存操作流流,改变字符串字母的大小写

 public static void main(String[] args) {
    String str="Hello world";
    //ByteArrayOutputStream 类的操作终端为内存
try(ByteArrayOutputStream bo=new ByteArrayOutputStream();
//字节输入内存流的操作终端为字符数组
ByteArrayInputStream bi=new ByteArrayInputStream(str.getBytes())){
    //设置缓冲字节数组
    byte[] buff = new byte[3];
    int len=-1;
    int count=0;
    //读取此输入流中的字节数组,每读一次就放入字节数组
    while((len=bi.read(buff))!=-1){
       for(int i=0;i<buff.length;i++){
           byte b=buff[i];
           if(b>'a'&&b<'z'){
               count++;
               //将小写转为大写
               buff[i]=(byte)( b-32);
           }
       }
       //每读一次,内存输出流从buff数组中输出写入到内存
       bo.write(buff,0,len);
    }
    //将内存输出流中缓存的字节数组存入新的数组
    byte[] newStr=bo.toByteArray();
    
    System.out.println(new String(newStr));
    System.out.println("转换次数"+count);
}catch (IOException e){
    e.printStackTrace();
}
    }

利用输入输出流、内存流完成两个文件的合并

 File file1 = Paths.get("D:", "TL-BITE", "Test", "write1.txt").toFile();
        File file2 = Paths.get("D:", "TL-BITE", "Test", "write2.txt").toFile();
        File file = Paths.get("D:", "TL-BITE", "Test", "write.txt").toFile();
        //将file1 file2 合并进file3
        try (FileInputStream fi1 = new FileInputStream(file1);
             FileInputStream fi2 = new FileInputStream(file2);
             FileOutputStream fo = new FileOutputStream(file);
             ByteArrayOutputStream bo = new ByteArrayOutputStream()
        ) {
            byte[] buff = new byte[3];
            int len = -1;
            //读此输入流中的内容(file1的内容)并将其存入buff数组中
            while ((len = fi1.read(buff)) != -1) {
            //每读一次,写入内存输出流
                bo.write(buff, 0, len);
            }
            //读此输入流中的内容(file2的内容)并将其存入buff数组中
            while ((len = fi2.read(buff)) != -1) {
            //每读一次,将其写入内存流
                bo.write(buff, 0, len);
            }
            //将buff数组(内存输出流)中的内容写入文件file
            fo.write(bo.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_42962924/article/details/84838851