@Test
public void testRandomAccessFile() throws IOException{
File file=new File("msg/550298242.txt");
RandomAccessFile raf=new RandomAccessFile(file, "rw"); //读取文件的对象

int code=-1;
try {
while((code=raf.read())!=-1){
System.out.print((char)code);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
raf.close();
}

@Test
public void rwTest() throws IOException{
File file=new File("msg/51232150298242.txt");
RandomAccessFile raf=new RandomAccessFile(file, "rw");
 try {
 String iwantosay=" 1 just do it";
 for(int i=0;i<iwantosay.length();i++){
raf.writeChar(iwantosay.charAt(i));
 }
 raf.writeChars("2  just do it ");
 raf.write(188);//it is not char
 raf.write('b');//it is not char
 raf.write('b');//it is not char
 raf.write('b');//it is not char
 raf.write('b');//it is not char
 raf.write('b');//it is not char
 raf.write('b');//it is not char
 raf.writeChars("3 hello nice to meet you");
 int a='丁';
 System.out.println(a);
 System.out.println(Integer.toBinaryString(a));

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

 
try {
int code=raf.read();
System.out.println(code);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

raf.close();
}

/**
* 测试写
* @throws IOException
*/
@Test
public void testWriteByte() throws IOException{
File file=new File("msg/51232150298242.txt");
RandomAccessFile raf=new RandomAccessFile(file, "rw");
String s="i服了u";
byte[] bytes=s.getBytes("utf-8");
for(byte b:bytes){
System.out.println(b);
}
raf.write(bytes);
System.out.println(System.getProperty("file.encoding"));
 

byte[] bytes_read=new byte[(int)file.length()];
raf.read(bytes_read);
for(byte b:bytes_read){
System.out.print(b+" ");
}
System.out.println(new String(bytes_read,"utf-8"));
raf.close();
}
/**
* 测试文件copy 
* 边读边写
* @throws IOException 
*/
@Test
public void copyFileTest() throws IOException{
File file=new File("msg/51232150298242.txt");
File file_read=new File("msg/51232150298242_copy.txt");

RandomAccessFile raf=new RandomAccessFile(file, "r");
RandomAccessFile raf_write=new RandomAccessFile(file_read, "rw");

byte[] bytes=new byte[1024];
int length=0;
while((length=raf.read(bytes))!=-1){
raf_write.write(bytes,0,length);
}
System.out.println("复制完成");
raf.close();
raf_write.close();


}

/**
* 文件指针
* @throws IOException
*/
@Test
public void filePointerTest() throws IOException{
File file=new File("msg/51232150298242.txt");
RandomAccessFile raf=new RandomAccessFile(file, "rw");
   
System.out.println(raf.getFilePointer());
 
   raf.write('H');
   System.out.println(raf.getFilePointer());
 
   raf.write("马里奥".getBytes("utf-8"));
   System.out.println(raf.getFilePointer()); 
  
   /* int code=raf.read();
    System.out.println(code);*/
    
   raf.seek(1);
   System.out.println(raf.getFilePointer());
   raf.write("玛".getBytes("utf-8"));
   
 /*  raf.seek(file.length());
   raf.write("no zuo no die".getBytes("utf-8"));
   System.out.println(raf.getFilePointer()); */
   
    raf.skipBytes(-5); 
    System.out.println("after-5:"+raf.getFilePointer());  
 
  
    raf.skipBytes(100); 
    System.out.println("after 100:"+raf.getFilePointer());  
    
    raf.skipBytes(6);
    System.out.println("after 6:"+raf.getFilePointer());  
   raf.write("no zuo no die".getBytes("utf-8"));
   System.out.println(raf.getFilePointer());  
   
}

本文转载:CSDN博客