I/O基础知识梳理

I/O流一共涉及以下函数类型

读出分为以下5种:
FileReader 读取文件内容(把文件转换成字符流读,读取出来的是char数组或者String )
BufferReader 包装FileReader 生成缓冲区 更高效
InputStream 是个接口(不能new InputStream);
FileInputStream 读取文件内容(字节流读,二进制数据,读取出来的是byte数组。)
InputStreamReader 将字节流转成字符流

写入分为相对应5种:
FileWriter
BufferedWriter
OutputStream
FileOutputStream
OutputStreamWriter
一定要区分开哪一个是写入,哪一个是读出
读出:
FileInputStream
FileReader
写入:
FileOutputStream
FileWriter

话不多说,直接上实例(复制代码直接try catch即可)

1.读取文件内容

FileReader 读取

 File file = new File("文件路径名");
        //读取文件的相关函数
        FileReader fileReader = null;
       fileReader = new FileReader("file");
        char c = 0;
        do {
            c=(char)fileReader.read();//读取一个字符
            System.out.println(c);
        }while (c != '-1');
    fileReader.close();

BufferReader 和FileReader 结合读取

 File file = new File("文件路径");
  BufferedReader bufferedReader=null;
       bufferedReader = new BufferedReader(new FileReader(file));
        String s;
        while((s=bufferedReader.readLine()) != null){
            System.out.println(s);
        }
        bufferedReader.close();
        //bufferedReader在close的时候会自动把FileReader给close掉

InputStreamReader new 出来FileInputStream读取
方式一

File file = new File("文件路径");
 InputStreamReader inputStreamReader = null;
       inputStreamReader = new InputStreamReader(new FileInputStream(file),"UTF-8");
        char[] ch=new char[2014];
		        int len=inputStreamReader.read(ch);
		        System.out.println(new String(ch,0,len));
		        inputStreamReader.close();
		        

在这里插入图片描述方法里的参数要传一个byte类型数组。
方式二

File file = new File("文件路径");
		InputStreamReader inputStreamReader = null;
		try {
			inputStreamReader = new InputStreamReader(new FileInputStream(file));
			 StringBuffer stringBuffer = new StringBuffer();
			 while(inputStreamReader.ready()) {
				 stringBuffer.append((char)inputStreamReader.read());
			 }
			 System.out.println(stringBuffer.toString());
		        inputStreamReader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

读取从键盘键入的内容

BufferReader 和InputStreamReader混合使用

按行读取


BufferedReader bufferedReader  = null;
bufferedReader  = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("当输入“退出”时退出输入:");
        String s = "";
        do {
            s = bufferedReader.readline();
            System.out.println(s);
        }while (c!="退出");
         bufferedReader.close();

按字符读取

BufferedReader bufferedReader = null;
bufferedReader  = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("当输入‘q’时退出输入:");
        char c;
        do {
            c = (char) bufferedReader.read();
            System.out.println(c);
        }while (c!='q');
                bufferedReader.close();

2.写入文件内容

FileWriter写入

File file = new File("文件路径");
FileWriter fileWriter = null;
        fileWriter = new FileWriter(file);
        fileWriter.append("插入内容");
        fileWriter.close();

BufferedWriter 和FileWriter 结合写入

File file = new File("文件路径");
BufferedWriter bufferedWriter = null;
      bufferedWriter = new BufferedWriter(new FileWriter(file));
        bufferedWriter.write("插入内容");
        bufferedWriter.close();

OutputStreamWriter new 出来FileOutputStream写入
如果FileOutputStream对象文件不存在则自动创建该文件

File file = new File("文件路径");
FileOutputStream fileOutputStream = new FileOutputStream("file ");
        OutputStreamWriter outputStreamWriter = null;
       outputStreamWriter = new OutputStreamWriter(fileOutputStream,"UTF-8");
        outputStreamWriter.append("插入内容");
        outputStreamWriter.append("\r\n");//换行
        outputStreamWriter.close();
        fileOutputStream.close();

利用接口OutputStream new 出来FileOutputStream配合其他文件写入
(在这里其他文件指Excel表格文件)

  OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream("workbook.xlsx");
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("firstSheet");
            Row row = sheet.createRow(0);
            row.createCell(0).setCellValue(1);
            workbook.write(outputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

使用WorkBook的write()方法时里面的参数必须是OutputStream

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值