zoukankan      html  css  js  c++  java
  • Java IO 简记

    1.File 类:

    java.io.File类:文件和目录路径名的抽象表示形式,与平台无关

    File 能新建、删除、重命名文件和目录,但 File 不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。

    File对象可以作为参数传递给流的构造函数

    File类的常见构造方法:

    • public File(String pathname)

             以pathname为路径创建File对象,可以是绝对路径或者相对路径,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储。

    • public File(String parent,String child)

              以parent为父路径,child为子路径创建File对象。

    File的静态属性String separator存储了当前系统的路径分隔符。在UNIX中,此字段为‘/’,在Windows中,为‘\’ 

    2.流的分类:

    按操作数据单位不同分为:字节流(8 bit),字符流(16 bit) 

    按数据流的流向不同分为:输入流,输出流

    按流的角色的不同分为:节点流,处理流

    Java的IO流共涉及40多个类,实际上非常规则,都是从如下4个抽象基类派生的

     

    >节点流可以从一个特定的数据源读写数据(P2P)

    (输入) FileInputStream、FileReader

    (输出) FileOutputStream、FileWriter 

    ----------

    >处理流是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能(缓冲流)

    (输入流):BufferedInputStream、BufferedReader

    (输出流):BufferedOutputStream、BufferedWriter

    为了提高数据读写的速度,Java API提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组

    根据数据操作单位可以把缓冲流分为:

    • BufferedInputStream 和 BufferedOutputStream
    • BufferedReader 和 BufferedWriter

    缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法

    对于输出的缓冲流,写出的数据会先在内存中缓存,使用flush()将会使内存中的数据立刻写出

    -----------

    >对象流

    ObjectInputStream和OjbectOutputSteam

    • 用于存储和读取对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。

    序列化(Serialize):用ObjectOutputStream类将一个Java对象写入IO流中

    反序列化(Deserialize):用ObjectInputStream类从IO流中恢复该Java对象

    • ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量

    对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。

    当其它程序获取了这种二进制流,就可以恢复成原来的Java对象序列化的好处在于可将任何实现了Serializable接口的对象转化为字节数据,使其在保存和传输时可被还原。

    注意:如果某个类的字段不是基本数据类型或 String  类型,而是另一个引用类型,那么这个引用类型必须是可序列化的,否则拥有该类型的 Field 的类也不能序列化

    -------------------

    程序中打开的文件 IO 资源不属于内存里的资源,垃圾回收机制无法回收该资源,所以应该显式关闭文件 IO 资源

    public class TestIO {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            File newFile = new File("D:/io.txt");
            InputStream inStream = null;
            OutputStream outStream = null;
            try {
                if (newFile.createNewFile()) {
                    System.out.println("文件创建成功");
                } else {
                    System.out.println("文件创建已存在");
                }
    
                inStream = new FileInputStream(newFile);
                int read = -1;
                while ((read = inStream.read()) != -1) {
                    System.out.print((char) read);
                }
    
                outStream = new FileOutputStream(newFile, true);
                outStream.write("Tttttt".getBytes());
                outStream.flush();
    
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    inStream.close();
                    outStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
        @Test
        public void testCharStream() {
            File newFile = new File("D:/io.txt");
            FileReader readerStream = null;
            FileWriter writerStream = null;
            try {
                if (newFile.createNewFile()) {
                    System.out.println("文件创建成功");
                } else {
                    System.out.println("文件创建已存在");
                }
    
                readerStream = new FileReader(newFile);
                int read = -1;
                while ((read = readerStream.read()) != -1) {
                    System.out.print((char) read);
                }
    
                writerStream = new FileWriter(newFile, true);
                writerStream.write("Tttttt");
                writerStream.flush();
    
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    readerStream.close();
                    writerStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
        @Test
        public void testBufferStream() {
            File newFile = new File("D:/io.txt");
            FileReader readerStream = null;
            FileWriter writerStream = null;
    
            try {
                if (newFile.createNewFile()) {
                    System.out.println("文件创建成功");
                } else {
                    System.out.println("文件创建已存在");
                }
    
                readerStream = new FileReader(newFile);
                int read = -1;
                while ((read = readerStream.read()) != -1) {
                    System.out.print((char) read);
                }
    
                writerStream = new FileWriter(newFile, true);
                writerStream.write("Tttttt");
                writerStream.flush();
                System.out.println("写入完成,重新读取。。。");
    
                BufferedReader bReader = new BufferedReader(readerStream);
                System.out.println(bReader.readLine() + "--");
                bReader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    readerStream.close();
                    writerStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
        @Test
        public void testObjectStream() throws FileNotFoundException, IOException, ClassNotFoundException {
            File file = new File("person.txt");
    
            // 序列化持久化对象
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
            Person person = new Person("Peter", 27);
            out.writeObject(person);
            out.close();
    
            // 反序列化,并得到对象
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
            Object newPerson = in.readObject(); // 没有强制转换到Person类型
            in.close();
            System.out.println(newPerson);
        }
    
        static class Person implements Serializable {
            private String name = null;
    
            private Integer age = null;
    
            public Person() {
                System.out.println("无参构造");
            }
    
            public Person(String name, Integer age) {
                this.name = name;
                this.age = age;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public Integer getAge() {
                return age;
            }
    
            public void setAge(Integer age) {
                this.age = age;
            }
    
            // getter setter方法省略...
            @Override
            public String toString() {
                return "[" + name + ", " + age + "]";
            }
        }
    
    }

     3.RandomAccessFile

    RandomAccessFile 类支持 “随机访问” 的方式,程序可以直接跳到文件的任意地方来读、写文件

    RandomAccessFile 对象包含一个记录指针,用以标示当前读写处的位置。RandomAccessFile 类对象可以自由移动记录指针:

    • long getFilePointer():获取文件记录指针的当前位置
    • void seek(long pos):将文件记录指针定位到 pos 位置

    构造器

     创建 RandomAccessFile 类实例需要指定一个 mode 参数,该参数指定 RandomAccessFile 的访问模式:

    • r: 以只读方式打开
    • rw:打开以便读取和写入
    • rwd:打开以便读取和写入;同步文件内容的更新
    • rws:打开以便读取和写入;同步文件内容和元数据的更新
    RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");
    raf.seek(5);
            
    //先读出来
    String temp = raf.readLine();
            
    raf.seek(5);
    raf.write("xyz".getBytes());
    raf.write(temp.getBytes());
            
    raf.close();
  • 相关阅读:
    爬虫基础1 怎么使用浏览器查看网络请求
    前端模块化总结(commonJs,AMD,CMD,ES6 Module)
    js强制类型转换规则
    vue-cli3中使用mxgraph的一些总结
    js概念笔记(this)
    js概念笔记(for循环,模块化)
    根据对象数组的某一属性排序
    从svg诞生的panda
    gulp笔记(从开发到发布)
    webpack4.x学习笔记
  • 原文地址:https://www.cnblogs.com/pjlhf/p/8663526.html
Copyright © 2011-2022 走看看