Java 随机访问文件示例

Java RandomAccessFile 提供了读取和写入文件的数据的功能. RandomAccessFile 与存储在文件系统中的大型文件( / 社区 / 教程 / string-byte-array-java )和一个标记器一起工作,我们可以使用它来移动文件指针位置。

Java 随机访问

Java RandomAccessFile, RandomAccessFile in java, Java RandomAccessFile read write append example RandomAccessFile class is part of Java IO. While creating the instance of RandomAccessFile in java, we need to provide the mode to open the file. For example, to open the file for read only mode we have to use "r" and for read-write operations we have to use "rw". Using file pointer, we can read or write data from random access file at any position. To get the current file pointer, you can call getFilePointer() method and to set the file pointer index, you can call seek(int i) method. If we write data at any index where data is already present, it will override it.

Java RandomAccessFile 阅读示例

我们可以从使用Java的RandomAccessFile的文件中读取字节数组. 下面是用RandomAccessFile使用的阅读文件(/community/tutorials/read-file-java-bufferedreader-fileinputstream-files-scanner-randomaccessfile)的伪代码。

1RandomAccessFile raf = new RandomAccessFile("file.txt", "r");
2raf.seek(1);
3byte[] bytes = new byte[5];
4raf.read(bytes);
5raf.close();
6System.out.println(new String(bytes));

在第一行中,我们正在为该文件创建 RandomAccessFile 实例以只读模式,然后在第二行中,我们正在将文件指示器移动到索引 1。我们创建了一个长度为 5 个字节的字节数组,所以当我们调用读(字节)方法时,然后从文件读到字节数组,最后,我们关闭了 RandomAccessFile 资源并将字节数组打印到控制台。

Java RandomAccessFile 写作示例

以下是一個簡單的例子,顯示如何使用Java中的RandomAccessFile來寫資料到檔案。

1RandomAccessFile raf = new RandomAccessFile("file.txt", "rw");
2raf.seek(5);
3raf.write("Data".getBytes());
4raf.close();

由于RandomAccessFile将该文件视为字节数组,所以写作操作可以对数据进行排列,并且可以附加到文件中. 这一切都取决于文件指示器的位置。 如果指示器被移动到文件长度之外,然后叫做写作操作,那么在文件中会写出垃圾数据。

随机接入 附件 示例

我们需要的是确保文件指针是在文件的末尾附加到文件. 下面是代码附加到文件使用随机AccessFile。

1RandomAccessFile raf = new RandomAccessFile("file.txt", "rw");
2raf.seek(raf.length());
3raf.write("Data".getBytes());
4raf.close();

Java 随机访问 示例

这里有一个完整的Java RandomAccessFile示例,具有不同的读写操作。

 1package com.journaldev.files;
 2
 3import java.io.IOException;
 4import java.io.RandomAccessFile;
 5
 6public class RandomAccessFileExample {
 7
 8    public static void main(String[] args) {
 9    	try {
10    		// file content is "ABCDEFGH"
11    		String filePath = "/Users/pankaj/Downloads/source.txt"; 
12    		
13    		System.out.println(new String(readCharsFromFile(filePath, 1, 5)));
14
15    		writeData(filePath, "Data", 5);
16    		//now file content is "ABCDEData"
17    		
18    		appendData(filePath, "pankaj");
19    		//now file content is "ABCDEDatapankaj"
20    	} catch (IOException e) {
21    		e.printStackTrace();
22    	}
23    }
24
25    private static void appendData(String filePath, String data) throws IOException {
26    	RandomAccessFile raFile = new RandomAccessFile(filePath, "rw");
27    	raFile.seek(raFile.length());
28    	System.out.println("current pointer = "+raFile.getFilePointer());
29    	raFile.write(data.getBytes());
30    	raFile.close();
31    	
32    }
33
34    private static void writeData(String filePath, String data, int seek) throws IOException {
35    	RandomAccessFile file = new RandomAccessFile(filePath, "rw");
36    	file.seek(seek);
37    	file.write(data.getBytes());
38    	file.close();
39    }
40
41    private static byte[] readCharsFromFile(String filePath, int seek, int chars) throws IOException {
42    	RandomAccessFile file = new RandomAccessFile(filePath, "r");
43    	file.seek(seek);
44    	byte[] bytes = new byte[chars];
45    	file.read(bytes);
46    	file.close();
47    	return bytes;
48    }
49
50}

这就是Java中的 RandomAccessFile的全部。

Published At
Categories with 技术
Tagged with
comments powered by Disqus