java使用IO读写文件

https://www.cnblogs.com/qiaoyeye/p/5383723.html

java读写文件的IO流分两大类,字节流和字符流,基类分别是字符:Reader和Writer;字节:InputStream和OutPutStream,

1、字符流分为FileReader和FileWrtier,这两个的父类是InputStreamReader和OutStreamWrtier

2、字节流分为FileInputStream和FileOutPutStream

关系网:

Reader->InputStreamReader->FileReader

Reader->BufferedReader字符流


 

Writer->OutputStreamWriter->FileWriter

Writer->BufferedWriter


 

InputStream->FileInputStream。FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader。

InputStream->FilterInputStream->BufferedInputStream


 

 

OutputStream->FileOutputStream。FileOutputStream 用于写入诸如图像数据之类的原始字节的流。要写入字符流,请考虑使用 FileWriter

OutputStream->FilterOutputStream->BufferedOutputStream

流的程序一般分以下四步:

1.创建文件对象 File file = new File("xxx.txt");

2.用流装载文件 FileReader fr = new FileReader(file);

3.如果用缓冲区,则用缓冲区装载流,用缓冲区是为了提高读写性能 BufferReader br = new BufferReader(fr);

4.开始读写操作 String s = null; StringBuffer sb = new StringBuffer(); while((s=br.readLine()!=null) { sb.append(s); } 如果遇到字节流要转换成字符流,则在缓冲区前加一步 InputStreamReader isr = new InpuStreamReader(InputStream in); 或者需要编码转换的,则在缓冲区前加一步 InputStreamReader isr = new InpuStreamReader(InputStream in,Charset cs);

下面的代码分别是复制文本文件、图像文件。字节转换字符的操作:

复制代码
package com.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import com.sun.org.apache.bcel.internal.generic.NEW;

/** 
 * @author QiaoJiafei 
 * @version 创建时间:2016年4月12日 下午12:03:37 
 * 类说明 
 */
public class TestIO {

    public static void main(String[] args) throws IOException {
        testFileReader();
        testFileInputStream();
    }
    private static void testInputStream() throws IOException {
        InputStream ist = System.in;
        BufferedReader br = new BufferedReader(new InputStreamReader(ist,"uft-8"));
        String s = "";
        while ((s=br.readLine())!=null) {
            System.out.println(s);
        }
        br.close();
    }
    
    private static void testFileReader() throws IOException {
        FileReader reader = new FileReader("/Users/qiaojiafei/Documents/test.txt");
        FileWriter writer = new FileWriter("/Users/qiaojiafei/Documents/test_copy.txt");
        
        BufferedReader bReader = new BufferedReader(reader);
        BufferedWriter bWriter = new BufferedWriter(writer);
        
        String temp = "";
        while((temp=bReader.readLine())!=null) {
            bWriter.write(temp);
        }
        bWriter.flush();
        bWriter.close();
        bReader.close();
    }
    private static void testFileInputStream() throws IOException {
        FileInputStream fis = new FileInputStream("/Users/qiaojiafei/Documents/zz.png");
        FileOutputStream fos = new FileOutputStream("/Users/qiaojiafei/Documents/zz_copy.png");
        
        BufferedInputStream bInputStream = new BufferedInputStream(fis);
        BufferedOutputStream bOutputStream = new BufferedOutputStream(fos);
        
        int temp = 0;
        while((temp=bInputStream.read()) != -1) {
            bOutputStream.write(temp);
        }
        bOutputStream.flush();
        bOutputStream.close();
        bInputStream.close();
    }
}
posted on 2018-12-03 14:18  花溪月影  阅读(529)  评论(0编辑  收藏  举报