Java基础: IO流

1.文件创建三种方式
package com.li.springboot.text;

import java.io.File;
import java.io.IOException;

public class Text {
    public static void main(String[] args) throws IOException {

        //方式一
        String filePath1 = "e:\\oop.txt";
        File file1 = new File(filePath1);
        file1.createNewFile();

        //方式二
        String filePath2 = "e:\\a.txt";
        File file2 = new File("e:", "a.txt");
        file2.createNewFile();

        //方式三
        String filePath3 = "e:";
        File ParentFile = new File(filePath3);
        File file = new File(ParentFile, "b.txt");
        file.createNewFile();

    }
}
2.文件目录创建
package com.li.springboot.text;

import java.io.File;
import java.io.IOException;

public class Text {
    public static void main(String[] args) throws IOException {
        //创建文件目录
        String filePath1 = "e:\\oop\\01";
        String filePath2 = "e:\\01";
        File file1 = new File(filePath1);
        File file2 = new File(filePath2);
        file1.mkdirs();//多级目录
        file2.mkdir();//一级目录
    }
}
3.字节流

FileInputStream 和 FileOutputStream

package com.li.springboot.text;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Text {
    public static void main(String[] args) throws IOException {
        String fileInPath = "e:\\oop.txt";
        String fileOutPath ="e:\\ioc.txt";
        FileInputStream fileInputStream = new FileInputStream(fileInPath);
        FileOutputStream fileOutputStream = new FileOutputStream(fileOutPath);
        byte[] bytes = new byte[1024];
        int read;
        while ((read = fileInputStream.read(bytes)) != -1){
            fileOutputStream.write(bytes,0,read);
        }
        if(fileInputStream != null){
            fileInputStream.close();
        }
        if(fileOutputStream != null){
            fileOutputStream.close();
        }
    }
}
4.字符流

FileReader 和 FileWriter

package com.li.springboot.text;

import java.io.*;

public class Text {
    public static void main(String[] args) throws IOException {
        String fileInPath = "e:\\oop.txt";
        String fileOutPath ="e:\\ioc.txt";
        FileReader fileReader = new FileReader(fileInPath);
        FileWriter fileWriter = new FileWriter(fileOutPath);
        char[] chars = new char[10];
        int read;
        while((read = fileReader.read(chars)) != -1){
            fileWriter.write(chars,0,read);
        }
        if(fileReader != null){
            fileReader.close();
        }
        if(fileWriter != null){
            fileWriter.close();
        }
    }
}
5.包装流 
字符包装流
package com.li.springboot.text;

import java.io.*;

public class Text {
    public static void main(String[] args) throws IOException {
        String fileInPath = "e:\\oop.txt";
        String fileOutPath ="e:\\ioc.txt";
        FileReader fileReader = new FileReader(fileInPath);
        FileWriter fileWriter = new FileWriter(fileOutPath);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        String readLine;
        while ((readLine = bufferedReader.readLine()) != null){
            bufferedWriter.write(readLine);
            bufferedWriter.newLine();
        }
        if(bufferedReader != null){
            bufferedReader.close();
        }
        if(bufferedWriter != null){
            bufferedWriter.close();
        }
    }
}
字节包装流
package com.li.springboot.text;

import java.io.*;

public class Text {
    public static void main(String[] args) throws IOException {
        String fileInPath = "e:\\oop.txt";
        String fileOutPath ="e:\\ioc.txt";
        FileInputStream fileInputStream = new FileInputStream(fileInPath);
        FileOutputStream fileOutputStream = new FileOutputStream(fileOutPath);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        byte[] bytes = new byte[1024];
        int read;
        while ((read = bufferedInputStream.read(bytes)) != -1){
            bufferedOutputStream.write(bytes,0,read);
        }
        if(bufferedInputStream != null){
            bufferedInputStream.close();
        }
        if(bufferedOutputStream != null){
            bufferedOutputStream.close();
        }
    }
}

6.对象处理流

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值