java IO流

来源:互联网 发布:五金手册软件下载 编辑:程序博客网 时间:2024/05/22 05:07

java.io.File 类

1、凡是与输入、输出相关的类、接口等都定义在Java.io 包下 
2、File是一个类,可以有构造器创建其对象。此对象对应着一个文件(.txt .avi .doc .ppt .mp3)或文件目录 
3、File类对象是与平台无关的。 
4、File中的方法,仅涉及到如何创建、删除、重命名等。对于内容,必须用IO流完成。 
5、File类的对象常作为io流的具体类的构造器的形参。 
6、File的静态属性String separator存储了当前系统的路径分隔符。在UNIX中,此字段为‘/’,在Windows中,为‘\’

File类下的方法 
访问文件名: 
getName() 
getPath() 
getAbsoluteFile() 
getAbsolutePath() 
getParent() 
renameTo(File newName) 重命名为//file1.renameTo(file2):file1重命名为file2.要求:file1文件一定存在,file2一定不存在

文件检测 
exists() 
canWrite() 
canRead() 
isFile() 
isDirectory()

获取常规文件信息 
lastModified() 
length()

文件操作相关 
createNewFile() 
delete()

目录操作相关 
mkDir() 创建一个文件目录。只有在上层文件目录存在的情况下,才能返回true 
mkDirs() 创建一个文件目录。若上层文件目录不存在,一并创建 
list() 
listFiles()

Java IO原理

什么是标准的I/O流? 
在java语言中,用stdin表示键盘,用stdout表示监视器。他们均被封装在System类的类变量in 和out中,对应于系统调用System.in和System.out。这样的两个流加上System.err统称为标准流,它们是在System类中声明的3个类变量: 
public static InputStream in 
publicstaticPrintStream out 
public static PrintStream err

流的分类

  • 按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)
  • 按数据流的流向不同分为:输入流,输出流
  • 按流的角色的不同分为:节点流,处理流

节点流可以从一个特定的数据源读写数据 
处理流是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能。

IO 流体系 

只有 FileInputStream、FileReader、FileOutputStream、FileWriter 是节点流,下面的都是处理流

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

主要流介绍

字节流 FileInputStream、FileOutputStream

//按字节读文件,当执行到文件结尾时,返回-1public void testFileInputStream1() throws IOException{        File file=new File("Hello.txt");        FileInputStream fis=new FileInputStream(file);//      int b=fis.read();//读取文件的一个字节,当执行到文件结尾时,返回-1//      while(b!=-1){//          System.out.println((char)b);//          b=fis.read();//      }        int b;        while((b=fis.read())!=-1){            System.out.println((char)b);        }        fis.close();    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
//按字节流实现文件复制的方法public void copyFile(String src,String dest){                //1、提供读入和写出的文件                File file1=new File(src);                File file2=new File(dest);                //2、提供相应的流                FileInputStream fis=null;                FileOutputStream fos=null;                try{                    fis=new FileInputStream(file1);                    fos=new FileOutputStream(file2);                    //3、实现文件的复制操作                    byte[] b=new byte[20];//一次送20个字节,大小会影响效率                    int len;                    while((len=fis.read(b))!=-1){                        fos.write(b, 0, len);                        //fos.write(b);  fos.write(b,0,b.length);  两种错误的写法                    }                }catch(Exception e){                    e.printStackTrace();                }finally{                    if(fos!=null){                        try {                            fos.close();                        } catch (IOException e) {                            e.printStackTrace();                        }                    }                    if(fis!=null){                        try {                            fis.close();                        } catch (IOException e) {                            e.printStackTrace();                        }                    }                }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37


字符流:FileReader和FileWriter

//使用FileReader 和 FileWriter 可以实现文本文件的复制    //对于非文本文件(视频文件,音频文件,图片文件),只能使用字节流public void testFileReaderWriter(){        //1.输入流对应的文件一定要存在        FileReader fr=null;        FileWriter fw=null;        try{            File src=new File("Hello.txt");            File dest=new File("Hello1.txt");            fr=new FileReader(src);            fw=new FileWriter(dest);            char[] c=new char[24];//一次送的字符数,大小会影响效率            int len;            while((len=fr.read(c))!=-1){                fw.write(c,0,len);            }        }catch(Exception e){            e.printStackTrace();        }finally{            if(fw!=null){                try {                    fw.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }            if(fr!=null){                try {                    fr.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37


缓冲流->属于处理流->能提高效率 
字节缓冲流 BufferedInputStream、BufferedOutputStream 
字符缓冲流 BufferedReader、BufferedWriter

// 使用BufferedInputStream和BufferedOutputStream实现非文本文件的复制    @Test    public void testCopyFile() {        // 1.提供读入、写出的文件        File file1 = new File("1.png");        File file2 = new File("2.png");        BufferedInputStream bis = null;        BufferedOutputStream bos = null;          try {            // 2.先创建相应的节点流,FileInputStream、FileOutputStream            FileInputStream fis = new FileInputStream(file1);            FileOutputStream fos = new FileOutputStream(file2);            // 3.将创建的节点流的对象作为形参传递给缓冲流的构造器中            bis = new BufferedInputStream(fis);            bos = new BufferedOutputStream(fos);            // 4.具体的实现文件复制的操作            byte[] b = new byte[1024];            int len;            while ((len = bis.read(b)) != -1) {                bos.write(b, 0, len);                bos.flush();            }        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally {            // 5、关闭相应的流            if (bos != null) {                try {                    bos.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }            if (bis != null) {                try {                    bis.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
//使用BufferedReader和BufferedWriter 实现文本文件复制    public void testBufferedReader(){        BufferedReader br=null;        BufferedWriter bw=null;        try {            File file=new File("Hello.txt");            File file1=new File("Hello5.txt");            FileReader fr=new FileReader(file);            FileWriter fw=new FileWriter(file1);            br = new BufferedReader(fr);            bw=new BufferedWriter(fw);//          char[] c=new char[1024];//          int len;//          while((len=br.read(c))!=-1){//              String str=new String(c,0,len);//              System.out.println(str);//          }            String str=null;            while((str=br.readLine())!=null){                System.out.println(str);                bw.write(str);                bw.newLine();                bw.flush();            }        }catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally{            if(bw!=null){                try {                    bw.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }            if(br!=null){                try {                    br.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48


如何实现字符流与字节流之间的转化? 
转换流:InputStreamReader OutputStreamWriter 
编码:字符串-》字符数组 
解码:字符数组-》字符串

转换流的编码应用 
可以将字符按指定编码格式存储。 
可以对文本数据按指定编码格式来解读。 
指定编码表的动作由构造器完成。

public void test1(){        //解码        File file=new File("Hello.txt");        BufferedReader br=null;        BufferedWriter bw=null;        try {            FileInputStream fis=new FileInputStream(file);            InputStreamReader isr=new InputStreamReader(fis,"GBK");            br = new BufferedReader(isr);            //编码            File file1=new File("Hello6.txt");            FileOutputStream fos=new FileOutputStream(file1);            OutputStreamWriter osw= new OutputStreamWriter(fos,"GBK");            bw = new BufferedWriter(osw);            String str=null;            while((str=br.readLine())!=null){                bw.write(str);                bw.newLine();                bw.flush();            }        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally{            if(bw!=null){                try {                    bw.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }            if(br!=null){                try {                    br.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

标准的输入输出流 
标准的输出流:System.out 
标准的输入流;System.in

//从键盘输入字符串,转化为大写,直到输入e或exit退出public void test2(){        BufferedReader br=null;        try {            InputStream is=System.in;            InputStreamReader isr=new InputStreamReader(is);            br = new BufferedReader(isr);            String str;            while(true){                System.out.println("请输入字符串");                str=br.readLine();                if(str.equalsIgnoreCase("e")||str.equalsIgnoreCase("exit")){                    break;                }                String str1=str.toUpperCase();                System.out.println(str1);            }        } catch (IOException e) {            e.printStackTrace();        }finally{            if(br!=null){                try {                    br.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

打印流 
PrintStream(字节打印流)和PrintWriter(字符打印流) 
提供了一系列重载的print和println方法,用于多种数据类型的输出 
PrintStream和PrintWriter的输出不会抛出异常 
PrintStream和PrintWriter有自动flush功能 
System.out返回的是PrintStream的实例

//打印流:字节流 PrintStream  字符流PrintWriter    @Test    public void printStreamWriter(){        FileOutputStream fos=null;        try {            fos=new FileOutputStream(new File("print.txt"));        } catch (FileNotFoundException e) {            e.printStackTrace();        }        //创建打印输出流,设置为自动刷新模式(写入换行符或字节'\n'时都会刷新输出缓冲区)        PrintStream ps=new PrintStream(fos,true);        if(ps!=null){            System.setOut(ps);//改变输出方式,默认是控制台        }        for(int i=0;i<=255;i++){//制造内容,输出ASCII字符            System.out.print((char)i);            if(i%50==0){//每50个数据一行                System.out.println();//换行            }        }        ps.close();    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

数据流 
为了方便地操作Java语言的基本数据类型的数据,可以使用数据流。

//数据流:用来处理基本数据类型、String、字节数组的数据:DataInputStream和DataOutputStream    @Test    public void testData(){        DataOutputStream dos=null;        try {            FileOutputStream fos=new FileOutputStream("data.txt");            dos = new DataOutputStream(fos);            dos.writeUTF("我爱你");            dos.writeBoolean(true);            dos.writeLong(454646656);        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally{            if(dos!=null){                try {                    dos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    @Test    public void testData1(){        DataInputStream dis=null;        try {            dis=new DataInputStream(new FileInputStream(new File("data.txt")));//          byte[] b=new byte[20];//          int len;//          while((len=dis.read(b))!=-1){//              System.out.println(new String(b,0,len));//          }            String str=dis.readUTF();            System.out.println(str);            boolean b=dis.readBoolean();            System.out.println(b);            long l=dis.readLong();            System.out.println(l);        } catch (IOException e) {            e.printStackTrace();        }finally{            if(dis!=null){                try {                    dis.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

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

对象序列化(Serialize):允许把内存中的Java对象转换成平台无关的二进制字节流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。当其它程序获取了这种二进制流,就可以恢复成原来的Java对象

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

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

凡是实现Serializable接口的类都有一个表示序列化版本标识符的静态变量:

  • private static final long serialVersionUID;
  • serialVersionUID用来表明类的不同版本间的兼容性
  • 如果类没有显示定义这个静态变量,它的值是Java运行时环境根据类的内部细节自动生成的。若类的源代码作了修改,serialVersionUID 可能发生变化。故建议,显示声明
public class TestObjextInputOutputStream {    //对象的反序列化过程:将硬盘中的文件通过ObjectiveInputStream转换为相应的对象    @Test    public void testObjextInputStream(){        ObjectInputStream ois=null;        try {            ois=new ObjectInputStream(new FileInputStream("person.txt"));            Person p1=(Person) ois.readObject();            System.out.println(p1);            Person p2=(Person) ois.readObject();            System.out.println(p2);        } catch (IOException e) {            e.printStackTrace();        } catch (ClassNotFoundException e) {            e.printStackTrace();        }finally{            if(ois!=null){                try {                    ois.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    //对象的序列化过程,将内存中的对象通过ObjectOutputStream转换为二进制流,存储在硬盘文件中    @Test    public void testObjectOutputStream(){        Person p1=new Person("小明",23);        Person p2=new Person("红米",21);        ObjectOutputStream oos=null;        try {            oos = new ObjectOutputStream(new FileOutputStream("person.txt"));            oos.writeObject(p1);            oos.flush();            oos.writeObject(p2);            oos.flush();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally{            if(oos!=null){                try {                    oos.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }}/*要实现序列化的类:  1.要求此类是可序列化的,实现Serializable接口  2.要求类的属性同样要实现Serializable接口  3.提供一个版本号:private static final long serialVersionUID=45646;  4.使用static或transient修饰的属性,不可实现序列化 */class Person implements Serializable{    private static final long serialVersionUID=45646;    String name;    Integer age;    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;    }    @Override    public String toString() {        return "Person [name=" + name + ", age=" + age + "]";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88


RandomAccessFile 类

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

  • 支持只访问文件的部分内容
  • 可以向已存在的文件后追加内容

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

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

构造器 
public RandomAccessFile(File file, String mode) 
public RandomAccessFile(String name, String mode)

  • r: 以只读方式打开
  • rw:打开以便读取和写入
  • rwd:打开以便读取和写入;同步文件内容的更新
  • rws:打开以便读取和写入;同步文件内容和元数据的更新
    //进行文件的读、写    @Test    public void test1(){        RandomAccessFile raf1=null;        RandomAccessFile raf2=null;        try {            raf1=new RandomAccessFile(new File("hello.txt"), "r");            raf2=new RandomAccessFile(new File("hello1.txt"), "rw");            byte[] b=new byte[20];            int len;            while((len=raf1.read(b))!=-1){                raf2.write(b, 0, len);            }        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally{            if(raf2!=null){                try {                    raf2.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if(raf1!=null){                try {                    raf1.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    //覆盖相应位置后面的字符    @Test    public void test2(){        RandomAccessFile raf=null;        try {            raf=new RandomAccessFile(new File("hello1.txt"), "rw");            raf.seek(3);//找到3的位置            raf.write("xy".getBytes());//在覆盖相应字节数        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally{            if(raf!=null){                try {                    raf.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    //实现插入的效果,在d字符后面插入"xy"    @Test    public void test3(){        RandomAccessFile raf=null;        try {            raf=new RandomAccessFile(new File("hello1.txt"), "rw");            raf.seek(4);//找到d的位置            String str=raf.readLine();//          long l=raf.getFilePointer();//          System.out.println(l);            raf.seek(4);            raf.write("xy".getBytes());//在覆盖相应字节数            raf.write(str.getBytes());        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally{            if(raf!=null){                try {                    raf.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }
原创粉丝点击