• Day15_IO流(上)


    Day15_IO流(上)

    File类

    File.separator表示路径里的“/”,在跨平台编程中需要用这个符号表示路径。

    File类中,创建一层目录用mkdir(),创建多层目录用mkdirs()。

    遍历文件夹

    import java.io.File;
    
    public class Demo01 {
        public static void main(String[] args) {
            //创建一个File类对象,将文件夹的路径复制进去
            File f=new File("/Users/Desktop/java");
            System.out.println(f);
            //遍历文件夹
            bianli(f,1);
    
        }
        public static void bianli(File f,int level){
            File[] files=f.listFiles();
            for(File file:files){
                for (int i = 0; i < level; i++) {
                    System.out.print("-");
                }
                System.out.println(file.getName());
                if(file.isDirectory()){
                    bianli(file,level+1);
                }
            }
        }
    }
    

    输出:

    /Users/Desktop/java
    -.DS_Store
    -java面经.pdf
    -code
    --.DS_Store
    --Hello.class
    --Hello.java
    -Blog
    --Day06_java对象上.md
    --学习安排.md
    --.DS_Store
    --Day10_数组(下).md
    --Day02mac下java卸载.md
    --Day03_流程控制.md
    --Day05_java数组.md
    --常用类(下).md
    --Day14_数据结构和集合(下).md
    --Day04_java方法.md
    --Day08_网络编程(上).md
    --Day07_java对象下.md
    --Day13_数据结构.md
    --Day09_数组(上).md
    --Day01.md
    --Day11_包装类(上).md
    --Day03_java基础知识.md
    

    IO流引入

    四个抽象类,abstract class

    三种分类方法

    1.大小2.方向 输入 输出
    字节 InputStream OuputStream
    字符 Reader Writer

    3.处理方式

    处理流:管套着管---流结合流

    节点流:直接和源文件或者目标文件接触的流

    文件字节流

    字符知识:1字符=2字节=16位

    读文件

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    public class Demo02 {
        public static void main(String[] args) {
            //文件---》程序
            //1.确定一个文件
            File f=new File("/Users/Desktop/java/test/Hello.txt");
            //2.将"一个管放进"源文件中
            try {
                FileInputStream fis = new FileInputStream(f);
                //对读取的文件大小进行预估
                System.out.println(fis.available());
                //3.开始"动作"
                int n=fis.read();
                while(n!=-1){
                    System.out.print(n+"	");
                    n=fis.read();
                }
                System.out.println();
                System.out.println("-----------------");
                //一种高效读取方法
                //定义缓冲数组,数组长度8,那么下面就读8个字节
                byte[] b=new byte[8];
                //返回值m代表这个数组中被占用的数量
                FileInputStream fis1 = new FileInputStream(f);
                int m=fis1.read(b);
                while(m!=-1){
                    for (int i = 0; i <m ; i++) {
                        System.out.print(b[i]+"	");
                    }
                    m=fis1.read(b);
                }
                fis1.close();
                fis.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    输出

    30
    228	189	160	229	165	189	239	188	129	232	167	129	229	136	176	228	189	160	229	190	136	233	171	152	229	133	180	239	188	129	
    -----------------
    -28	-67	-96	-27	-91	-67	-17	-68	-127	-24	-89	-127	-27	-120	-80	-28	-67	-96	-27	-66	-120	-23	-85	-104	-27	-123	-76	-17	-68	-127	
    

    写文件

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class Demo03 {
        public static void main(String[] args) throws IOException {
            //1.确定一个文件:目标文件
            File f=new File("/Users/Desktop/java/test/b.txt");
            //2.将管子放到目标文件中
            FileOutputStream fos=new FileOutputStream(f);
            //如果用空构造器,文件直接覆盖;如果用两个参数的构造器,设置true,在文件内容结尾追加
            //FileOutputStream fos=new FileOutputStream(f,true);
            //3.给一个字符串
            String str="abc你好";
            //4.开始动作
            byte[] bytes=str.getBytes();
            for (byte b:bytes) {
                fos.write(b);
            }
            //关闭fos流
            fos.close();
        }
    }
    

    运行结果:生成b.txt文件,将abc你好写入了b.txt文件中。

    复制文件

    import java.io.*;
    
    public class Demo04 {
        public static void main(String[] args) throws IOException {
            //1.确定源文件
            File f1=new File("/Users/Desktop/java/test/b.txt");
            //2.确定目标文件
            File f2= new File("/Users/Desktop/java/test/c.txt");
            //3.将一个管放入源文件中
            FileInputStream fis = new FileInputStream(f1);
            //4.将一个管放入目标文件中
            FileOutputStream fos=new FileOutputStream(f2);
            //5.开始动作
          //利用字节流,源文件和目标文件各访问了文件(内容所占的字节数)次。
    //        int n=fis.read();
    //        while(n!=-1){
    //            fos.write(n);
    //            n=fis.read();
    //        }
            //高效动作
           //利用缓冲数组,源文件和目标文件各访问了文件(内容长度/缓冲数组的长度)次。
            byte[] b=new byte[6];
            int len=fis.read(b);
            while(len!=-1){
                //0-len保证只把有效的字节复制进c.txt
                fos.write(b,0,len);
                len=fis.read(b);
            }
            //6.关闭流:先开后关
            fos.close();
            fis.close();
    
        }
    }
    

    运行结果:生成c.txt文件,将b.txt文件的内容复制进了b.txt文件中。

    文件字符流

    复制文件

    import java.io.*;
    
    public class Demo05 {
        public static void main(String[] args) throws IOException {
            //确定源文件
            File f1=new File("/Users/Desktop/java/test/c.txt");
            //确定目标文件
            File f2=new File("/Users/Desktop/java/test/d.txt");
            //3.将一个管放入源文件中
            FileReader fr = new FileReader(f1);
            //4.将一个管放入目标文件中
            FileWriter fw=new FileWriter(f2);
            //5.开始动作
          //利用字节流,源文件和目标文件各访问了文件(内容的字符数)次。
    //        int n=fr.read();
    //        while(n!=-1){
    //            fw.write(n);
    //            n=fr.read();
    //        }
            //用char格式读取文件内容
            //利用缓冲数组,源文件和目标文件各访问了文件(内容长度/缓冲数组的长度)次。
            char[] ch=new char[4];
            int len=fr.read(ch);
            while(len!=-1){
                fw.write(ch,0,len);
                len=fr.read(ch);
            }
            //6.关闭流:字符流必须进行刷新,如果写的是关闭流,那么底层直接帮你做了flush动作
            //fw.flush();
            fw.close();
            fr.close();
        }
    }
    

    运行结果:生成d.txt文件,将c.txt文件的内容复制进了d.txt文件中。

    缓冲流

    缓冲流的作用就是把数据先放在一个地方,然后再把数据一次性交付。

    缓冲字节流

    复制文件
    import java.io.*;
    
    public class Demo06 {
        public static void main(String[] args) throws IOException {
            //确定源文件
            File f1=new File("/Users/Desktop/java/test/d.txt");
            //确定目标文件
            File f2=new File("/Users/Desktop/java/test/e.txt");
            //3.直接接触的是字节流,字节流站在工作第一线,直接跟源文件,目标文件接触
            FileInputStream fis=new FileInputStream(f1);
            FileOutputStream fos=new FileOutputStream(f2);
            //字节流外面包着缓冲字节流
            BufferedInputStream bis=new BufferedInputStream(fis);
            BufferedOutputStream bos=new BufferedOutputStream(fos);
            //开始动作
          //利用缓冲字节流,源文件和目标文件各只访问了一次。
            byte[] b=new byte[4];
            int n=bis.read(b);
            while(n!=-1){
                bos.write(b,0,n);
                n=bis.read(b);
            }
            //关闭流:只关闭高级流就可以,低级流会自动关闭。但是为了保险,在执行一次关闭低级流的语句
            bos.close();
            bis.close();
            fos.close();
            fis.close();
        }
    }
    

    运行结果:生成e.txt文件,将d.txt文件的内容复制进了e.txt文件中。

    缓冲字符流

    复制文件
    import java.io.*;
    
    public class Demo07 {
        public static void main(String[] args) throws IOException {
            //确定源文件
            File f1 = new File("/Users/Desktop/java/test/e.txt");
            //确定目标文件
            File f2 = new File("/Users/Desktop/java/test/f.txt");
            //3.直接接触的是字符流,字符流站在工作第一线,直接跟源文件,目标文件接触
            FileReader fr = new FileReader(f1);
            FileWriter fw = new FileWriter(f2);
            //字符流外面包着缓冲字符流
            BufferedReader br = new BufferedReader(fr);
            BufferedWriter bw = new BufferedWriter(fw);
            //开始动作
            String str=br.readLine();
            while (str != null) {
                bw.write(str);
                //加换行
                // bw.write("
    ");
                //bw.newLine();
                str=br.readLine();
            }
            //关闭流:只关闭高级流就可以,低级流会自动关闭。但是为了保险,在执行一次关闭低级流的语句
            bw.close();
            br.close();
            fw.close();
            fr.close();
        }
    }
    

    运行结果:生成f.txt文件,将e.txt文件的内容复制进了f.txt文件中。

  • 相关阅读:
    sort color (荷兰国旗)
    先序遍历和后序遍历构建二叉树
    二叉树的遍历
    排序
    内存相关内容
    chrome控制台console方法表
    记一次移动端CSS引发的小Bug
    JavaScript的事件
    浅谈webpack打包原理
    JS模块化进程
  • 原文地址:https://www.cnblogs.com/gaoyao/p/13599638.html
Copyright © 2020-2023  润新知