IO流

File类

File类是一个文件或者文件夹的路径(可以是存在的,也可以是不存在),既可以是相对路径也可以是绝对路径.

相对路径和绝对路径

绝对路径:
从盘符开始的路径
如: /Users/wangqi/IdeaProjects/javase/day08/a.txt
相对路径:
以项目的根目录为父路径,直接写子路径即可
项目的根目录为: /Users/wangqi/IdeaProjects/javase
如: day08/a.txt

遍历文件夹

 public static void printDirectory(File file){
        File[] files = file.listFiles();
        if (files!=null){
            for (File file1 : files) {
                if (file1.isDirectory()){
                    printDirectory(file1);
                }else {
                    System.out.println(file1.getName());
                }
            }
        }
    }

IO流

IO流是用来出路数据之间的数据传输
输入流:读取数据
输出流:写入数据
字节流:可以读取任何类型的数据
字符流:只能读取文本文件

字节流一次读取一个数据在控制台输

public class Demo {
    public static void main(String[] args) throws Exception{
        FileInputStream fis = new FileInputStream("D:\\Idea-workplace\\寒假复习\\File对象与IO流\\a.txt");
        int len;
        while ((len=fis.read())!=-1){
            System.out.print((char)len);
        }
    }
}

字符流一次读写一个数据

public class Demo2 {
    public static void main(String[] args) throws Exception{
        FileInputStream fis = new FileInputStream("D:\\Idea-workplace\\寒假复习\\File对象与IO流\\a.txt");
        FileOutputStream fos = new FileOutputStream("D:\\Idea-workplace\\寒假复习\\File对象与IO流\\b.txt");
        //一个一个的读写
        int len;
        while ((len=fis.read())!=-1){
            fos.write(len);
        }
        fos.close();
        fis.close();
    }
}

字符流一次读写多个数据

public class Demo3 {
    public static void main(String[] args) throws Exception{
        FileInputStream fis = new FileInputStream("D:\\Idea-workplace\\寒假复习\\File对象与IO流\\a.txt");
        FileOutputStream fos = new FileOutputStream("D:\\Idea-workplace\\寒假复习\\File对象与IO流\\b.txt");
        int len;
        byte[] bytes = new byte[1024];
        while ((len=fis.read(bytes))!=-1){
            fos.write(bytes,0,len);
        }
        fos.close();
        fis.close();
    }
}

字节缓冲流一次读写一个数据

public class Demo1 {
    public static void main(String[] args) throws Exception {
        InputStream is = new BufferedInputStream(new FileInputStream("D:\\Idea-workplace\\寒假复习\\File对象与IO流\\a.txt"));
        OutputStream os = new BufferedOutputStream(new FileOutputStream("D:\\Idea-workplace\\寒假复习\\File对象与IO流\\b.txt"));
        int len;
        while ((len=is.read())!=-1){
            os.write(len);
        }
        os.close();
        is.close();
    }
}

字节缓冲流一次读写多个字节

public class Demo2 {
    public static void main(String[] args) throws Exception{
        InputStream is = new BufferedInputStream(new FileInputStream("D:\\Idea-workplace\\寒假复习\\File对象与IO流\\a.txt"));
        OutputStream os = new BufferedOutputStream(new FileOutputStream("D:\\Idea-workplace\\寒假复习\\File对象与IO流\\b.txt"));
        int len;
        byte[] bytes = new byte[1024];
        while ((len=is.read(bytes))!=-1){
            os.write(bytes,0,len);
        }
        os.close();
        is.close();
    }
}

字符流一次复制一个字符

public class Demo1 {
    public static void main(String[] args) throws Exception{
        FileReader fr = new FileReader("D:\\Idea-workplace\\寒假复习\\File对象与IO流\\a.txt");
        FileWriter fw = new FileWriter("D:\\Idea-workplace\\寒假复习\\File对象与IO流\\b.txt");
        int ch;
        while ((ch = fr.read())!=-1){
            fw.write(ch);
        }
        fw.close();
        fr.close();
    }
}

字符流一次复制多个字符

public class Demo2 {
    public static void main(String[] args) throws Exception{
        FileReader fr = new FileReader("D:\\Idea-workplace\\寒假复习\\File对象与IO流\\a.txt");
        FileWriter fw = new FileWriter("D:\\Idea-workplace\\寒假复习\\File对象与IO流\\b.txt");
        int ch;
        char[] chars = new char[1024];
        while ((ch=fr.read(chars))!=-1){
            fw.write(chars,0,ch);
        }
        fw.close();
        fr.close();
    }
}

字符缓冲流一次复制一个字符

public class Demo1 {
    public static void main(String[] args) throws Exception{
        Reader r = new BufferedReader(new FileReader("D:\\Idea-workplace\\寒假复习\\File对象与IO流\\a.txt"));
        Writer w = new BufferedWriter(new FileWriter("D:\\Idea-workplace\\寒假复习\\File对象与IO流\\b.txt"));
        int len;
        while ((len=r.read())!=-1){
            w.write(len);
        }
        w.close();
        r.close();
    }
}

字符缓冲流一次复制一行

public class Demo {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("D:\\Idea-workplace\\寒假复习\\File对象与IO流\\a.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\Idea-workplace\\寒假复习\\File对象与IO流\\b.txt"));
        String line;
        while ((line=br.readLine())!=null){
            bw.write(line);
        }
        bw.close();
        br.close();
    }
}

字符转换流一次复制多个字符

//按照指定的UTF-8编码来读取a.txt文件
Reader r=new InputStreamReader(new FileInputStream("day09/a.txt"),"UTF-8");
//按照指定的GBK编码来写入b.txt文件
Writer w=new OutputStreamWriter(new FileOutputStream("day09/b.txt"),"GBK");//一边读一边写
char[] chs=new char[1024];int len;
 //记录每次读取到的有效字符个数
 while((len=r.read(chs))!=-1){  
 //把有效的字符写入文件中  
 w.write(chs,0,len);
 }//释放资源
 w.close();r.close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值