字节io流、字符io流

  字节流(byte)

字节输出流对象

 字节输出流OutputStream

复制代码
创建字节流输出对象,明确目的地
//构造方法,如果描述的文件不存在则创建,存在则覆盖
    FileOutputStream fos=new FileOutputStream("D:\io0512\byte.txt",true);
    //写出一个字节
       fos.write(100);
    //写字节数组
    byte[] bytes={49,48,48};
    fos.write(bytes);
    fos.write(bytes, 1, 2);
    //写汉字
    byte[] bb={-66,-67,-68,-69};
    fos.write("你好
小小".getBytes());//换行
    //释放资源
    fos.close();
复制代码

 在地址后面加上,true开来开启续写模式,否则就是执行一次覆盖一次;

处理异常

复制代码
FileOutputStream fos=null;
    
    try {
        fos=new FileOutputStream("D:\io0512\byte.txt",true);
        fos.write(100);
        //写字节数组
        byte[] bytes={49,48,48};
        fos.write(bytes);
        fos.write(bytes, 1, 2);
        //写汉字
        byte[] bb={-66,-67,-68,-69};
        fos.write("你好
小小".getBytes());//换行
        
        
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    }
复制代码

  字节输入流InputStream

                               一个字节一个字节读

复制代码
public class Demo03 {//输入流
public static void main(String[] args) throws IOException {
    //创建字节输入流对象,明确数据源
    FileInputStream fis=new FileInputStream("D:\io0512\byte.txt");
    //一个字节一个字节读
    int len=0;
    /*while(len!=-1){
    System.out.println((char)len);
    len=fis.read();
    }*/
    while((len=fis.read())!=-1){
        System.out.println((char)len);
        
        }
    fis.close();
}
}
复制代码

数组读

复制代码
public class Demo04 {
public static void main(String[] args) throws IOException {
    FileInputStream fis=new FileInputStream("D:\io0512\byte.txt");
    byte[] bytes=new byte[2];
    int len=0;
    
    while((len=fis.read(bytes))!=-1){
    
    System.out.println(new String(bytes,0,len));//从0开始,转len个
    }
    fis.close();
}
}
复制代码

                               复制文件

原理;读取一个已有的数据,并将这些读到的数据写入到另一个文件中。

 从一个文件夹向另一个文件夹复制,一个字节一个字节复制

复制代码
public class Demo05 {
public static void main(String[] args) throws IOException {
    //
    FileInputStream fis=new FileInputStream("D:\io0512\byte.txt");
    
    FileOutputStream fos=new FileOutputStream("D:\io0512\a\byte.txt");
    
    int len=0;
    while((len=fis.read())!=-1){
        fos.write(len);
    }
    fis.close();
    fos.close();
}
}
复制代码

一个数组一个数组复制

复制代码
public class Demo06 {
public static void main(String[] args) throws IOException {
    FileInputStream in=new FileInputStream("D:\io0512\byte.txt");
    FileOutputStream on=new FileOutputStream("D:\io0512\a\byte2.txt");
    int len=0;
    byte[] bytes=new byte[2];
    while((len=in.read(bytes))!=-1){
        on.write(bytes,0,len);
    }
    in.close();
    on.close();
}
}
复制代码

    字符流(char)

                字符编码表

编码表:其实就是生活中字符和计算机二进制的对应关系表。

1、ascii: 一个字节中的7位就可以表示。对应的字节都是正数。0-xxxxxxx

2、iso-8859-1:拉丁码表 latin,用了一个字节用的8位。1-xxxxxxx  负数。

3、GB2312:简体中文码表。包含6000-7000中文和符号。用两个字节表示。两个字节第一个字节是负数,第二个字节可能是正数

                GBK:目前最常用的中文码表,2万的中文和符号。用两个字节表示,其中的一部分文字,第一个字节开头是1,第二字节开头是0

               GB18030:最新的中文码表,目前还没有正式使用。

1、unicode:国际标准码表:无论是什么文字,都用两个字节存储。

l  Java中的char类型用的就是这个码表。char c = 'a';占两个字节。

l  Java中的字符串是按照系统默认码表来解析的。简体中文版 字符串默认的码表是GBK。

5、UTF-8:基于unicode,一个字节就可以存储数据,不要用两个字节存储,而且这个码表更加的标准化,在每一个字节头加入了编码信息(后期到api中查找)。

能识别中文的码表:GBK、UTF-8;正因为识别中文码表不唯一,涉及到了编码解码问题。

对于我们开发而言;常见的编码 GBK  UTF-8  ISO-8859-1

文字--->(数字) :编码。 “abc”.getBytes()  byte[]

(数字)--->文字  : 解码。 byte[] b={97,98,99}  new String(b,0,len)

                字符输入流Reader

   FileReader类

复制代码
public class CharStreamDemo {
    public static void main(String[] args) throws IOException {
        //给文件中写中文
        writeCNText();
        //读取文件中的中文
        readCNText();
    }    
    //读取中文
    public static void readCNText() throws IOException {
        FileReader fr = new FileReader("D:\test\cn.txt");
        int ch = 0;
        while((ch = fr.read())!=-1){
            //输出的字符对应的编码值
            System.out.println(ch);
            //输出字符本身
            System.out.println((char)ch);
        }
    }
    //写中文
    public static void writeCNText() throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\test\cn.txt");
        fos.write("欢迎你".getBytes());
        fos.close();
    }
}
复制代码

                字符输出流Writer

   FileWriter类

 

    FileWriter写入中文到文件中

复制代码
public class FileWriterDemo {
    public static void main(String[] args) throws IOException {
        //演示FileWriter 用于操作文件的便捷类。

         FileWriter shuchu=new FileWriter("D:\io0512\write.txt");//加true不覆盖,续写
         shuchu.write(100);
         shuchu.flush();//字符流刷
         char[] c={'你','中','a','0'};
         shuchu.write(c);
         shuchu.write(c,1,1);
         shuchu.write("海绵宝宝");

        shuru.write("你好谢谢再见");//这些文字都要先编码。都写入到了流的缓冲区中。

        shuru.flush();


         shuchu.close();

        
    }
}
复制代码

                flush()和close()的区别?

flush():将流中的缓冲区缓冲的数据刷新到目的地中,刷新后,流还可以继续使用。

close():关闭资源,但在关闭前会将缓冲区中的数据先刷新到目的地,否则丢失数据,然后在关闭流。流不可以使用。如果写入数据多,一定要一边写一边刷新,最后一次可以不刷新,由close完成刷新并关闭。

    复制文本文件

一个字符一个字符读,写

复制代码
public class Demo04 {
public static void main(String[] args) throws IOException {
    FileReader shuru=new FileReader("D:\io0512\write.txt");
    FileWriter shuchu=new FileWriter("D:\io0512\a\write.txt");
    int len=0;
    while((len=shuru.read())!=-1){
        shuchu.write(len);
        shuchu.flush();
    }
    shuru.close();
    shuchu.close();
}
}
复制代码

一个数组一个数组读,写

复制代码
public class Demo05 {
    //字符流,只能操作文本文件
public static void main(String[] args) throws IOException {
    FileReader shuru=new FileReader("D:\io0512\write.txt");
    FileWriter shuchu=new FileWriter("D:\io0512\a\write.txt");
    int len=0;
    char[] c=new char[2];
    while((len=shuru.read(c))!=-1){
        shuchu.write(new String(c,0,len) );
        shuchu.flush();
    }
    shuru.close();
    shuchu.close();
}
}
原文地址:https://www.cnblogs.com/marswenze/p/13426082.html