java-字节流-字符流

 

I/O叙述

 

 

 

 

 

 

FileOutputStream类字节输出流的介绍:

 

 

 写入数据的原理

    java程序-->JVM(java虚拟机)--->OS(操作系统)----》OS调用写数据的方法---》把数据写入到文件钟

 

字节输出的使用步骤:

11创建一个FileOutputStream 对象,构造方法钟传递写入数据的目的地

22调用FileOutputStream对象中的方法write把数据写入到文件中

33释放资源,因为流使用会占用一定的内存,使用完毕要把内存情况,提高程序的效率

 

public class Demo1 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("C:\\Users\\quan\\Desktop\\untitled1\\src\\fs.txt");
        fos.write(97);
        fos.close();

    }
}

 

 

 

一次写多个字节 

 

 

 

public class Demo1 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("C:\\Users\\quan\\Desktop\\untitled1\\src\\bfs.txt");
        fos.write(49);
        fos.write(48);
        fos.write(48);
        //100
        byte[] bytes = {65,66,57,59};
        fos.write(bytes);

        byte[] bytes1 = {-65,-66,57,59};
        fos.write(bytes1);
        fos.close();//100AB9;烤9;

    }
}

 

 

字符数组的一部分写入文件

 

 

 

public class Demo1 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("C:\\Users\\quan\\Desktop\\untitled1\\src\\cfs.txt");
        byte[] bytes1 = {65,66,57,59};
        fos.write(bytes1,1,2);
        fos.close();
        //B9
    }
}

 

写入字符串,利用字符串方法转换为字节

 

 

 

public class Demo1 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("C:\\Users\\quan\\Desktop\\untitled1\\src\\cfs.txt");
         byte[]  bytes = "halo".getBytes();
        fos.write(bytes);
        fos.close();
        //文件:halo
    }
}

 

 

 

数据追加写入

 

 

 

 

public class Demo1 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("C:\\Users\\quan\\Desktop\\untitled1\\src\\cfs.txt",true);
         byte[]  bytes = "halo".getBytes();
        fos.write(bytes);
        fos.close();
        //文件:halohalo
    }
}

 

 

换行

public class Demo1 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("C:\\Users\\quan\\Desktop\\untitled1\\src\\cfs.txt",true);
         byte[]  bytes = "halo".getBytes();
         for (int i =1;i<=10;i++){
             fos.write(bytes);
             fos.write("\r\n".getBytes());
         }

        fos.close();
        //文件:halohalo
    }
}
/*
halohalohalo
halo
halo
halo
halo
halo
halo
halo
halo
halo

 */

 

 

 

 

FileInputStream类字节输入流的介绍:

 

 

 

 

 

 

public class Demo1 {
    public static void main(String[] args) throws IOException {
        FileInputStream fos = new FileInputStream("C:\\Users\\quan\\Desktop\\untitled1\\src\\cfs.txt");
        int i = fos.read();
        System.out.println(i);

        int i2 = fos.read();
        System.out.println(i2);
        fos.close();
    }
}
/*
104
97
 */

 

循环文件

public class Demo1 {
    public static void main(String[] args) throws IOException {
        FileInputStream fos = new FileInputStream("C:\\Users\\quan\\Desktop\\untitled1\\src\\cfs.txt");
        //循环读取:
        int len =0;
        //因为读到没有数据的时候,自动返回-1
        while ((len = fos.read())!= -1){
            System.out.print(len);
            System.out.println((char)len);
        }
    }
}
/*
104h
97a
108l
111o
13
10

104h
97a
108l
111o
13
10
 */

 

读取文件的原理

 

 

 

 

字节输入流一次多字节

 

 

 方法参数byte[]作用就是存储每次读取到的多个字节

数组的长度一班定义为1024或者他的整数倍

方法的返回参数int是  每次读取到的所有有效字节个数

public class Demo1 {
    public static void main(String[] args) throws IOException {
        FileInputStream fos = new FileInputStream("C:\\Users\\quan\\Desktop\\untitled1\\src\\cfs.txt");
        byte[] bytes = new byte[3];
        int len = fos.read(bytes);
        System.out.println(len);
        System.out.println(Arrays.toString(bytes));
        System.out.println(bytes);
        System.out.println(new String(bytes));//直接转为字符串
        fos.close();
    }
}
/*
3
[104, 97, 108]
[B@1b6d3586
hal
Arrays.toString()的作用是用来很方便地输出数组,
而不用一个一个地输出数组中的元素。

 */

 

 

 

 

 

字符流

当使用字节流读取文本的时候,可能会有一个小问题,就是遇到中文字符时,可能不会显示完整的字符。

是因为一个中午字符可能占用多个字节存储,所以提供了一些字符流类,以字符为单位读写数据,专门处理文本文件

 

/*
使用字节流读取中文文件
1个中文
    GBK:占用两个字符
    UTF-8占用3个字符
 */

import java.io.FileInputStream;
import java.io.IOException;

public class Demo11 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("C:\\Users\\quan\\Desktop\\练习\\src\\code\\haotusay");
        int len = 0;
        while ((len = fis.read())  !=  -1){
            System.out.println(len);
        }
        fis.close();
    }
}

 

 

 

 

 

 

 

 

/*
字符输入流的使用步骤:
创建FileReader 对象,构造方法钟绑定要读取的数据源
使用FileReader对象钟的方法read读取文件夹
释放资源
 */

 

public class Demo11 {
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("C:\\Users\\quan\\Desktop\\练习\\src\\code\\haotusay");
//        int len = 0;
//        while ((len = fr.read()) != -1){
//            System.out.print((char)len);
//        }//结果:全植强45
        char[] cs = new char[1024];//存储读取到的多个字符
        int len = 0;
        while ((len = fr.read(cs)) != -1){
            System.out.println(len);
            System.out.println(new String(cs,0,len));//因为到尾部的时候的值就为-1
        }


        fr.close();
    }
}

 

 

 

 

 

 

字符输出流

 

/*
字符输出流的使用步骤
    创建FileWriter 对象,构造方法钟绑定要写入数据的目的地
    使用Filewriter方法writer,把数据写入到内存缓冲区钟(字符转换为字节的过程)
    使用FIlewriter方法flush,把内存缓冲区钟的数据,刷新到文件中
    释放资源-会先把内存缓冲区中的数据刷新到文件中
 */

 

 

 写入单个字符

public class Demo11 {
    public static void main(String[] args) throws IOException {
        FileWriter fr = new FileWriter("C:\\Users\\quan\\Desktop\\练习\\src\\code\\haotusay");
        fr.write(98);//直接替换文件内容
        fr.flush();//同一次的写入追加写入
fr.close(); } }
     /*
            flush:刷新缓冲区,流对象可以继续使用
            close:先刷新缓冲区,然后通知系统释放资源。流对象不可以在被使用
         */

写入的其他方法

public class Demo11 {
    public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("C:\\Users\\quan\\Desktop\\练习\\src\\code\\haotusay");
        char[] cs = {'a','v','c','d'};
        fw.write(cs);
        fw.write(cs,1,3);
        fw.write("  quanzhiqiang  ");
        fw.write("货拉拉拉拉",1,3);
        fw.close();
        //avcdvcd  quanzhiqiang  拉拉拉
    }
}

 

 

续写和换行

 

 

public class Demo11 {
    public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("C:\\Users\\quan\\Desktop\\练习\\src\\code\\haotusay",true);
//        for (int i = 0;i<10 ;i++){
//            fw.write("qq" + i);
//        }//QQqq0qq1qq2qq3qq4qq5qq6qq7qq8qq9

        for (int i = 0;i<10 ;i++){
            fw.write("qq" + i+"\n");
        }
        /*
        QQqq0qq1qq2qq3qq4qq5qq6qq7qq8qq9qq0
qq1
qq2
qq3
qq4
qq5
qq6
qq7
qq8
qq9
         */
        fw.close();
    }
}

 

posted @ 2020-06-12 07:58  小丑quan  阅读(194)  评论(0)    收藏  举报