Java IO学习--RandomAccessFile

1、什么是 随机访问文件流 RandomAccessFile

这个类在很多资料上翻译成中文都是:随机访问文件,在中文里,随机是具有不确定的含义,指一会访问这里,一会访问那里的意思。如果以这种语义来解释的话,就会感到很困惑。其实,Random在英文中不仅仅有随机,还有任意的意思。如果中文名为任意访问文件是不是就会更好的理解。任意表示我们可以指定文件中任何一个位置去操作一个文件。

RandomAccessFile适用于大小已知的文件,能够随机的读取或者写入文件。可以通过使用seek()将记录从一处转移到另外一处,然后读取或者修改记录。文件中记录的大小不一定相同,只要我们能确定记录的大小和它们在文件的位置即可。

2、RandowAccessFile存在的价值

1、是JAVA I/O流体系中功能最丰富的文件内容访问类,它提供了众多方法来访问文件内容。

2、由于可以自由访问文件的任意位置,所以如果需要访问文件的部分内容,RandomAccessFile将是更好的选择。

3、可以用来访问保存数据记录的文件,文件的记录的大小不必相同,但是其大小和位置必须是可知的。

3、RandomAccessFile的使用

 1 public class RandomFileDemo {
 2     
 3     public static void main(String[] args) {
 4         
 5         try {
 6             RandomAccessFile raf = new RandomAccessFile(new File("E:\a.txt"), "rw");
 7             long offset = raf.getFilePointer();//文件指针
 8             long len= raf.length();//当前文件的字节大小
 9             System.out.println("offset:"+offset+" len:"+len);
10             //raf.seek(len);//移动到文件末尾,可进行追加文件内容
11             System.out.println("offset:"+raf.getFilePointer()+" len:"+raf.length());
12             raf.write(65);//1字节
13             raf.writeChar(97);//2字节
14             raf.writeChar('
');//2字节
15             raf.writeBytes("test");//4字节
16             System.out.println("offset:"+raf.getFilePointer()+" len:"+raf.length());
17             
18             
19             raf.seek(0);//移动文件指针到开始位置
20             int c = raf.read();
21             System.out.println(c+"    "+(byte)c+"    "+(char)c);//从指定位置开始读取一个字节
22             
23             System.out.println(raf.readChar());//从文件指针(类似一个游标)位置开始读取一个字符(2个字节)
24             //再次回到文件的开始位置,读取一行
25             raf.seek(0);
26             System.out.print(Arrays.toString(raf.readLine().getBytes())+"    ");
27             System.out.println("offset:"+raf.getFilePointer()+" len:"+raf.length());
28             raf.close();
29             
30             
31             
32             
33         } catch (Exception e) {
34             e.printStackTrace();
35         }
36         
37     }
38 
39 }

结果输出(a.txt是空文件):

offset:0 len:0
offset:0 len:0
offset:9 len:9
65    65    A
a
[65, 0, 97, 0]    offset:5 len:9

4、进程控制和流

在java程序中执行其他操作系统的程序,并且控制这些程序的输入和输出。主要要是借助流来读取程序的输出。如下:

 1 public class ProcessDemo {
 2     
 3     public static void main(String[] args) {
 4         
 5         try {
 6             Process process = Runtime.getRuntime().exec(new String[]{"cmd.exe","/C","dir"});
 7             
 8             BufferedReader br =new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
 9             
10             BufferedReader br2 =new BufferedReader(new InputStreamReader(process.getErrorStream()));
11             String line=null;
12             String line2=null;
13             while((line=br.readLine())!=null){
14                 System.out.println(line);
15             }
16             while((line2=br2.readLine())!=null){
17                 System.out.println(line2);
18             }
19             
20             br.close();
21             br2.close();
22         } catch (IOException e) {
23             e.printStackTrace();
24         }
25         
26         
27     }
28 
29 }

输出结果:

驱动器 F 中的卷没有标签。
 卷的序列号是 3A48-2555

 F:NewStudyWorkSpacecoder_java 的目录

2018/03/19  19:31    <DIR>          .
2018/03/19  19:31    <DIR>          ..
2018/03/19  19:31             1,433 .classpath
2018/03/19  19:31               562 .project
2018/03/19  19:31    <DIR>          .settings
2018/04/05  18:05               702 pom.xml
2018/03/19  19:31    <DIR>          src
2018/03/19  19:31    <DIR>          target
               3 个文件          2,697 字节
               5 个目录 341,102,870,528 可用字节
原文地址:https://www.cnblogs.com/liupiao/p/9297577.html