Java隐藏文件的读写

Java 读写隐藏文件
java读写隐藏文件与普通的文件略有不同,如下:

如果使用:
FileOutputStream out = new FileOutputStream(file);
就会报错:拒绝访问
java.io.FileNotFoundException: **.txt (拒绝访问。)
at java.io.FileOutputStream.open(Native Method)

即使我们设置写权限也一样。可以看到java调用本地方法open,

所以就不能调用open方法,而改调用openAppend方法,就是追加:
所以我们可以使用new FileOutputStream(file, true) 来实现写入,但是只能追加写入,而不能进行修改操作。
publicvoid appendDatatoHiddenFile(String file) {
try {
FileOutputStream out = new FileOutputStream(file, true);
PrintStream p = new PrintStream(out);
for (int i = 0; i < 10; i++)
p.println("hello: " + i );
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

那么如何任意操作“隐藏文件”呢?使用使用RandomAccessFile
简单介绍一下RandomAccessFile:

输入流FileInputStream和输出流 FileOutputStream,实现的是对磁盘文件的顺序读写,而且读写要分别创建不同对象。相比之下RandomAccessFile类则可对文件实现随机读写操作。

如某个文件有30个字节,读取数据过程中,从20-30读取,用skip( )//跳过方法。
RandomAccessFile类,即可以充当输入也可充当输出流。可以看作节点流。 RandomAccessFile对象的文件位置指针遵循下面的规律;新建RandomAccessFile对象的文件位置指针位于文件的开头处;每次读写操作之后,文件位置的指针都相应后移到读写的字节数;可以通过getFilePointer方法来获得文件位置指针的位置,通过seek方法来设置文件指针的位置。


RandomAccessFile常用方法:
1. RandomAccessFile (”路径+文件名”, String“rw”/”r”)两个参数
2. Void close( )
3. Long length( )
4. Void seek( )
5. Long getFilePointer( )获得当前指针位置,默认为0
6. Int read( )从文件当前位置读取一个字节
7. int read (byte[]b)
8. int read (byte[]b,int off,int len)
9. Final boolean readBoolean( )从文件当前位置读取boolean类型的一个字节 boolean在内存占1/8
10. Final_ char readChar( )从文件中读取2个字节。
11. Final int readInt( )从文件中读取4个字节。
12. ##Final String readLine( )从文件中读取一行后转为String。
13. Void write(byte[]b)将字节数组B中的数据写到文件中。
14. Void write(byte[]b,int off,int len)将 len 个字节从指定字节数组写入到此文件,并从偏移量 off 处开始。
15. Void write(int b)将指定的数据写到文件中。
16. Final void writeBoolean(boolean v)将boolean类型的值按单字节的形式写到文件中0或1
17.

Final void writeChar(int v)将char值按2个字节写入到文件中
18. Final void writeChars(String s)将字符串按字符方式写入到文件中
19. Final void writeInt(int v)按四个字节将 int 写入该文件,先写高字节
20. skipBytes(long i):从前往后拨弄指示器的位置,就是跳过多少个字节读取数据。
21. Void seek(long p): 对指示器作决定性的定位,用于从后往前拨弄指示器的位置。对于seek方法,拥有skipBytes( )的功能,但seek( )在使用过程非常影响系统的开销。只有万不得已的情况下使用。

来个列子:
publicvoid randomAccessFile(String file) throws Exception {
RandomAccessFile f = new RandomAccessFile(file, "rw");
System.out.println("File.lelngth:" + (f.length()) + "B");
System.out.println("File PointPosition:" + f.getFilePointer());
f.seek(f.length());
f.writeBoolean(true);
f.writeBoolean(false);
f.writeChar('a');
f.writeChars("hello!");
System.out.println("File Length;" + (f.length()) + "B");

f.seek(0);
System.out.println(f.readBoolean());
System.out.println(f.readBoolean());

System.out.println(f.readLine());
f.close();
}

在来个处理中文的例子:
publicvoid chinesewrit(String toAppend) {

try {
// 写入
int i = 0;
String record = new String();
String toCn = null;
// 处理中文问题
toCn = new String(toAppend.getBytes("GBK"), "ISO8859_1");

RandomAccessFile rf = new RandomAccessFile("c:\\aaa.txt", "rw");
rf.seek(rf.length());
rf.writeBytes(toCn + "\n");
rf.close();
// 读取
RandomAccessFile rf2 = new RandomAccessFile("c:/aaa.txt", "r");
String outCn = null;
while ((record = rf2.readLine()) != null) {
i++;
// 处理中文问题
outCn = new String(record.getBytes("ISO8859_1"), "GBK");

System.out.println("Line " + i + ":" + outCn);
}
rf2.close();
} catch (Exception e) {
e.printStackTrace();
}
}

说明上面的例子都是append得,如果要覆盖的话,就seek(0),然后写入!

相关文档
最新文档