学习内容:
1、RandomAccessFile随机访问文件
RandomAccessFile可以随机读写文件,随机读写文件就是说可以任意访问文件的位置,这是其他流所不能操纵的。RandomAccessFile 类包含一个记录指针,用于标识当前流的读写位置,这个位置可以向前移动,也可以向后移动。RandomAccessFile包含两个方法来操作文件记录指针。
long getFilePoint():记录文件指针的当前位置。
void seek(long pos):将文件记录指针定位到pos位置。
RandomAccessFile类的构造函数如下。
public,RandomAccessFile (File file,String mode)throws FileNotFoundException public RandomAccessFile (String name,String mode)throws FileNotFoundException
在 RandomAccessFile中读写文件有四种模式如表所示
示例:使用RandomAccessFile随机读写文件
import java. io. IOException; import java. io. RandomAccessFile; public class RandomAccessFileDemo public static void main (String[] args) throws IOException { //创建对象 RandomAccessFile acf =new RandomAccessFile ("D: /Hello.txt", "rw"); //写byte数组 byte[] b = "Hello Java! !".getBytes() ; acf.write(b) ; //设置偏移量 acf.seek(6) ; System. out. println("pointer="+acf .getFilePointer()) ; byte[] b2="C++" .getBytes() ; acf.write (b2) ; int len=-1; byte[] buf=new byte [1024] ; //将偏移量设置成第一个位置 acf .seek(0) ; System. out. println ("pointer "+acf .getFilePointer()); while( (len=acf . read (buf)) !=-1) { String s =new String (buf,0, len) ; System. out.println(s) ; } } }
综合实例:
package helloworld; import java.io.*; public class IOUtils { public static void main(String[] args) { byte[] ary= {41,4,-2,(byte)0xfe,(byte)0xff}; for(int b:ary) { b&=0xff; if(b<=0xf) System.out.print("0"); System.out.print(Integer.toHexString(b)+" "); } System.out.println(); } public static byte[] read(String file) { try { InputStream in=new FileInputStream(file); byte[] buf=new byte[in.available()]; in.read(buf); in.close(); return buf; }catch(IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } public static Object deepCopy(Object obj) { try { ByteArrayOutputStream buf=new ByteArrayOutputStream(); ObjectOutputStream oos=new ObjectOutputStream(buf); oos.writeObject(obj); oos.close(); byte[] ary=buf.toByteArray(); ObjectInputStream ois=new ObjectInputStream(new ByteArrayInputStream(ary)); Object o=ois.readObject(); ois.close(); return o; }catch(Exception e) { throw new RuntimeException(e); } } public static void cp(File from,File to) { try { InputStream in=new FileInputStream(from); OutputStream out=new FileOutputStream(to); byte[] buf=new byte[1024]; int n; while((n=in.read(buf))!=-1) { out.write(buf,0,n); } in.close(); out.close(); }catch(IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } public static void cpl(File from,File to) { try { InputStream in=new FileInputStream(from); OutputStream out=new FileOutputStream(to); int b; while((b=in.read())!=-1) { out.write(b); } in.close(); out.close(); }catch(IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } public static void cp(String from,String to) { cp(new File(from),new File(to)); } /**将文件按照16进制形式打印到控制台,每16个byte为一行*/ public static void print(File file) { try { InputStream in=new FileInputStream(file); int b; int i=1; while((b=in.read())!=-1) { if(b<=0xf) System.out.print("0"); System.out.print(Integer.toHexString(b)+" "); if(i++%16==0) { System.out.println(); } } System.out.println(); in.close(); }catch(IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } public static void print(String file) { print(new File(file)); } public static void split(String file,int size) { try { if(size<=0) { throw new IllegalArgumentException("搞啥呀!"); } int idx=0; InputStream in=new BufferedInputStream(new FileInputStream(file)); OutputStream out=new BufferedOutputStream(new FileOutputStream(file+"."+idx++)); int b; int count=0; while((b=in.read())!=-1) { out.write(b); count++; if(count%(size*1024)==0) { out.close(); out=new BufferedOutputStream(new FileOutputStream(file+"."+idx++)); } } in.close(); out.close(); }catch(IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } public static void join(String file) { try { String filename=file.substring(0,file.lastIndexOf(".")); String num=file.substring(file.lastIndexOf(".")+1); int idx=Integer.parseInt(num); OutputStream out=new FileOutputStream(filename); File f=new File(filename+"."+idx++); while(f.exists()) { InputStream in=new FileInputStream(f); cp(in,out); in.close(); f=new File(filename+"."+idx++); } out.close(); }catch(IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } public static void cp(InputStream in,OutputStream out) throws IOException{ byte[] buf=new byte[1024*512]; int count; while((count=in.read(buf))!=-1) { out.write(buf,0,count); } out.flush(); } }