Android Day15 Java文件操作

具体内容

Ⅰ主要包括以下几个操作
image.png

Ⅱ基础内容

1.流的定义
流是一个抽象的概念,当Java程序需要从数据源读取数据时,会开启一个到数据源的流,数据源可以是文件,内存或者网络等,当程序需要输出数据到目的地时也一样会开启一个流,数据目的地也可以是文件、内存或者网络等,是为了更方便地处理数据的输入输出。

2字节流和字符流的区别
(1)字节流也称为原始数据,需要用户读入后进行相应的编码转换。而字符流的实现是基于自动转换的,读取数据时会把数据按照JVM的默认编码自动转换成字符。
(2)字符流处理的单元为2个字节的Unicode字符,分别操作字符、字符数组或字符串,而字节流处理单元为1个字节,操作字节和字节数组,所以字符流是由Java虚拟机将字节转化为2个字节的Unicode字符为单位的字符而成的。
(3)字节流可用于任何类型的对象,包括二进制对象,而字符流只能处理字符或者字符串,字节流提供了处理任何类型的IO操作的功能,但它不能直接处理Unicode字符,而字符流就可以

3.字符流、字节流的使用
如果是音频文件、图片、歌曲,使用字节流;如果是中文(文本),使用字符流更好

具体代码

Ⅰ创建file文件

  String path = "D:\\Android\\Javahellp\\java\\src\\main\\java\\Day8";        
  File file = new File(path.concat("\\1.txt"));
  //判断是否存在
   if (file.exists() == false) {
       //不存在就创建
        try {
      file.createNewFile();         
  } catch (IOException e) {
           System.out.println("IO异常了");
       }
   }

Ⅱ向文件写入数据 - 字节流
1.创建文件输出流对象

    FileOutputStream fos = new FileOutputStream(file);

2.调用write方法写入

   byte[] text = {'1', '2', '3', '4'};
    fos.write(text);

3.操作完毕需要关闭stream对象

     fos.close();
    //向文件写入数据 - 字符流
    FileWriter fw = new FileWriter(file);
    char[] text2 = {'安', '卓', '开', '发'};
    fw.write(text2);

    fw.close();

    //读取内容
    FileInputStream fis = new FileInputStream(file);

    byte[] name = new byte[12];
    int count = fis.read(name);

    fis.close();
    System.out.println(count + " " + new String(name));

    FileReader fr = new FileReader(file);
    char[] book = new char[4];
    count = fr.read(book);

    fr.close();
    System.out.println(count + " " + new String(book));

Ⅲ向文件里面存一个对象,序列化 serializable,保存的对象必须实现Serializable接口,如果对象内部还有属性变量是其他类的对象 这个类也必须实现Serializable接口

    Person xw = new Person();
    xw.name = "小王";
    xw.age = 20;

    OutputStream os = new FileOutputStream(file);
    ObjectOutputStream oos = new ObjectOutputStream(os);
    oos.writeObject(xw);
    oos.close();

Ⅳ从文件里面读取一个对象

    InputStream is = new FileInputStream(file);
    ObjectInputStream ois = new ObjectInputStream(is);
    Person xw = (Person) ois.readObject();

   System.out.println(xw.name + " " + xw.age);
   ois.close();

    long start = System.currentTimeMillis();

将一个文件 copy到另外一个位置
1.源文件的路径

   String sourcePath = "C:\\Users\\Gyf\\Desktop\\a.png";

2.目标文件的路径

        String desPath = "D:\\Android\\Javahellp\\java\\src\\main\\java\\Day8\\鸡哥.png";

3.图片 字节

    FileInputStream fis = new FileInputStream(sourcePath);
    FileOutputStream fos = new FileOutputStream(desPath);

    byte[] in = new byte[1024];

    int count = 0;
    while ((count = fis.read(in)) != -1) {
        fos.write(in, 0, count);
    }
   while (true) {
        int count = fis.read(in);
        if (count != -1) {
            //读取到内容
            //将这一次读取的内容写入到目标文件
            fos.write(in, 0, count);
        } else {
            break;
        }
    }
    fis.close();
    fos.close();

    long end = System.currentTimeMillis();
    System.out.println(end-start);

Ⅴ使用Buffered速度更快 保存图片视频的时候

    String sourcePath = "";
    String desPath = "";
    long start = System.currentTimeMillis();
    InputStream is = new FileInputStream(sourcePath);
    BufferedInputStream bis = new BufferedInputStream(is);

    OutputStream os = new FileOutputStream(desPath);
    BufferedOutputStream bos = new BufferedOutputStream(os);

    byte[] in = new byte[1024];
    int count = 0;
    while ((count = bis.read(in)) != -1) {
        bos.write(in, 0, count);
    }

    bis.close();
    bos.close();

    long end = System.currentTimeMillis();

    System.out.println(end-start);

你可能感兴趣的:(Android Day15 Java文件操作)