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)を使用することができます 書き込みを実現しますが、書き込みを追加するしかなく、修正操作はできません.
では、「隠しファイル」を任意に操作するにはどうすればいいのでしょうか.RandomAccessFileの使用
RandomAccessFileについて簡単に説明します.
入力ストリームFileInputStreamと出力ストリームFileOutputStreamは、ディスクファイルの順番の読み書きを実現し、読み書きはそれぞれ異なるオブジェクトを作成します.それに対してRandomAccessFileクラスでは、ファイルのランダムな読み書きが可能です.
あるファイルに30バイトがある場合は、データを読み出す過程で20-30から読み出し、skip()/スキップ方法を使用します.
RandomAccessFileクラスは、入力としても出力ストリームとしても機能します.ノードフローと見なすことができます.RandomAccessFileオブジェクトのファイル位置ポインタは以下の法則に従う.新しいRandomAccessFileオブジェクトのファイル位置ポインタは、ファイルの先頭にあります.読み書き操作のたびに、ファイルの位置のポインタは読み書きのバイト数に移動します.getFilePointerメソッドでファイル位置ポインタの位置を取得し、seekメソッドでファイルポインタの位置を設定できます.
列の子を指定します.
上の例を説明するのはすべてappend得で、上書きするならseek(0)、それから書きます!
転載を歓迎します:出典を明記して第1のポータルネットwww.jxc 114.comに仕入れて保存してください
記事のアドレス:http://www.jxc114.com/?action-viewspace-itemid-2901
使用する場合:
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 + "
");
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)、それから書きます!
転載を歓迎します:出典を明記して第1のポータルネットwww.jxc 114.comに仕入れて保存してください
記事のアドレス:http://www.jxc114.com/?action-viewspace-itemid-2901