欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAVA——常用IO流类

程序员文章站 2024-03-07 08:18:32
...

常用IO流类关系图

字节流:用来操作字节数据,字节流可以用来拷贝所有的数据,文件读和写,但是字节流读取中文有可能造成乱码。

字符流:字符流读取中文不会造成乱码,字符流可以用来拷贝(文件读和写)纯文本的数据,非纯文本的不能用字符流来拷贝,只能用字节流


JAVA——常用IO流类


常用流的用法例子

  • 普通纯文本复制粘贴方法
public boolean copyValueOfText(String url) throws IOException{
        FileReader reader = new FileReader(url);
        FileWriter writer = new FileWriter("b.txt");
        int num;
        char[] arr = new char[2];//一次读取两个字符
        while((num=reader.read(arr))!=-1){
            writer.write(String.copyValueOf(arr, 0, num));
        }
        reader.close();
        writer.close();
        return true;
    }

  • 高效纯文本复制粘贴方法
public boolean copyValueOfTextEffect(String url) throws IOException{

        BufferedReader reader = new BufferedReader(new FileReader(url));
        BufferedWriter writer = new BufferedWriter(new FileWriter("new.txt"));

        int num;
        char[] arr = new char[2];
        while((num=reader.read(arr))!=-1){
            writer.write(String.copyValueOf(arr, 0, num));
        }
        reader.close();
        writer.close();
        return true;
    }

  • 字节(万能)复制粘贴方法
public boolean copyValueOfFile(String url) throws IOException{

        FileInputStream fis = new FileInputStream(url);
        FileOutputStream fos = new FileOutputStream("new2.txt");
        int num;
        byte[] arr = new byte[2];
        while((num = fis.read(arr))!=-1){
            fos.write(arr, 0, num);
        }
        fis.close();
        fos.close();
        return true;        
    }

  • 高效字节(万能)复制粘贴方法
public boolean copyValueOfFileEffect(String url) throws IOException{
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(url));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("new2.txt"));
        int num;
        byte[] arr = new byte[2];
        while((num = bis.read(arr))!=-1){
            bos.write(arr, 0, num);
        }
        bis.close();
        bos.close();
        return true;        
    }

  • 转换流(可以避免不同编码的文件互相读写时发生的乱码问题(GBK-一个字符两个字节,UTF-8-一个字符三个字节),因为该流有一个特殊的构造方法可以指定流的编码类型)
public void changeStream() throws IOException{

        InputStreamReader isr = new InputStreamReader(new FileInputStream("FileDir/d/gbk"),"GBK");
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("FileDir/d/utf-8"),"UTF-8");

        int num;
        char[] arr = new char[2];

        while((num = isr.read(arr))!=-1){
            osw.write(new String(arr, 0, num));
        }
        isr.close();
        osw.close();
    }

  • 对象流
public void loadObjectByObjetInputStream() throws Exception{
        //要求该对象要实现序列化接口Java.io.Serializable
        TestObject testObject = new TestObject();

        testObject.setName("我的名字");
        testObject.setUsername("用户名");
        testObject.setPassword("用户密码");

        //把当前对象保存到文件中
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Object.ob"));
        oos.writeObject(testObject);
        //读取当前文件中保存的对象
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Object.ob"));
        TestObject to = (TestObject) ois.readObject();
        System.out.println(to.getName()+to.getUsername()+to.getPassword());

        oos.close();
        ois.close();

    }