java中的IO操作

来源:互联网 发布:强制卸载windows补丁 编辑:程序博客网 时间:2024/04/30 10:11

这部分内容比较容易忘记 所以写在这里省的下次再去找了,先来看张图,主要有字符流和字节流:
I/O流的分类

以下是一个边读边写的流的具体使用:

public static void main(String[] args) {    // TODO Auto-generated method stub    FileInputStream flin = null;    FileOutputStream fout = null;    File file = new File("f:1.txt");    if (!file.exists()) {        try {            file.createNewFile();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    try {        flin = new FileInputStream(                "E:/User/QrCode/MVP_Java/src/com/zxj/mvp/Login.java");        fout = new FileOutputStream(file);        // 方法一:缓存流大小一次读完        byte[] temp = new byte[flin.available()];        flin.read(temp);        System.out.println(new String(temp, "utf-8"));        fout.write(temp);        // 方法二:指定缓存大小        // int line = -1;        // // 一次读多个字节        // byte[] tmp = new byte[32 * 1024];        // // 读入多个字节到字节数组中,line为一次读入的字节数        // while ((line = flin.read(tmp)) != -1) {        // System.out.println(new String(tmp, "UTF-8"));        // fout.write(tmp);        // }        // 方法三:逐个字符读取        // while ((line = flin.read( )) != -1) {        // fout.write(line);        // }        System.out.println("finished!");    } catch (IOException e) {        // TODO Auto-generated catch block        e.printStackTrace();    } finally {        if (fout != null) {            try {                fout.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        if (flin != null) {            try {                flin.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }}
0 0
原创粉丝点击