美文网首页Android开发成长史
Android_开发_Day14_文件管理

Android_开发_Day14_文件管理

作者: 不要问我问就是百度 | 来源:发表于2019-08-14 17:16 被阅读1次

Android_开发Day14文件管理

目的:

学会文件的操作能够实时的保存一些数据,为以后的项目打下基础。

技术:

<1> 文件类:File类是文件类,用法就是:File file = new File(完整路径);文件类中要掌握的方法包括exists()判断文件是否存在,createNewFile()创造新的文件。

<2> i/o流:什么是i/o流呢,就是统一管理数据的写入和读取。向文件写入数据可以用FileOutputStream类,写入字节流用里面的write方法。同理读入可以用FileInputStream类里面的read方法,读出字节流。可以用FileWriter类写入字符流,FileReader类读出字符流,以上方法都是对应的,字节流只能用字节数组接收和写入,字符流只能用字符数组接收和写入,但是同一个文件既可以用字节流的方式读写,也可以用字符流的方式读写。注意:重复写入文件会覆盖原内容。

<3> 将对象存到文件:可以用 ObjectOutput类的writeObject方法存一个对象,但是该类的构造方法的参数是FileOutputStream的一个对象,同理读出文件用ObjectInputStream的readObject方法读取一个对象,同样该类的构造方法的参数是FileInputStream的一个对象,该类的readObject的返回值是一个Object类,需要手动转换成对应的类。

技术如何使用:

//创建文件
File file = new File("E:\\Android\\MyApplication1\\lib123\\src\\main\\java\\day9\\file1.txt");
        if (file.exists() == false){
            try {
                file.createNewFile();
            }
            catch (IOException e){
                System.out.println("IO异常");
            }
        }

创建文件

//写入文件
        
        //法一:写入字节文件
        FileOutputStream fos = new FileOutputStream(file);
        byte[] text = {'w','q','q','d','c','b','h','h','t','d'};
        fos.write(text);
        fos.close();
        //法二:写入字符文件
        FileWriter fw = new FileWriter(file);
        char[] str = {'重','点'};
        fw.write(str);
        fw.close();

写入文件

//读出文件

        //法一:以字节方式读出文件
        FileInputStream fis = new FileInputStream(file);
        byte[] text1 = new byte[50];
        fis.read(text1);
        System.out.println(new String(text1));
        //法二:以字符的形式读取文件
        FileReader fr = new FileReader(file);
        char[] str1 = new char[10];
        fr.read(str1);
        System.out.println(new String(str1));

读出文件并检查是否正确

//写入对象
        HG hg = new HG("御坂10032号", 19);
        ObjectOutputStream oop = new ObjectOutputStream(new FileOutputStream(file));
        oop.writeObject(hg);
        oop.close();

储存对象

//读取对象

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        HG hg1 = (HG)ois.readObject();
        System.out.println(hg1.name+" "+hg1.age);

读出对象
有了这些工具我们就可以做一个复制粘贴的功能,代码如下:

String sourcePath = "D:\\360极速浏览器下载\\图片\\63-1Z429144F1933.jpg";
        String target = "C:\\Users\\芒果\\Desktop\\63-1Z429144F1933.jpg";
        FileInputStream fileInputStream = new FileInputStream(sourcePath);
        FileOutputStream fileOutputStream = new FileOutputStream(target);
        byte[] in = new byte[1024];
        while (true){
            int count = fileInputStream.read(in);
            if (count != -1){
                fileOutputStream.write(in,0,count);
            }
            else {
                break;
            }
        }
        fileInputStream.close();
        fileOutputStream.close();

如果需要换目标文件和目标地址,只需要修改sourcePath和target就行了。

实际使用效果:

批注 2019-08-14 165825.jpg 批注 2019-08-14 165608.jpg 批注 2019-08-14 170101.jpg 批注 2019-08-14 170031.jpg 对象的读写1.jpg 对象的读写2.jpg

总结:

文件的输入和输出分3种,字节(可以是任何类型的文件,如:图片,视频等),字符(一般是文本文件),对象(根据实际需求进行输入输出)。

相关文章

网友评论

    本文标题:Android_开发_Day14_文件管理

    本文链接:https://www.haomeiwen.com/subject/xjewjctx.html