See More

------------------------ RandomAccessFile | ------------------------ # Ëæ»úIO # ¹¹Ôì·½·¨ RandomAccessFile(File file, String mode) RandomAccessFile(String name, String mode) * file/name Ä¿±êÎļþ * mode ioģʽ:"r","w"×éºÏ # ʵÀý·½·¨ FileChannel getChannel() * »ñÈ¡¹ØÁªµÄchannel FileDescriptor getFD() long getFilePointer() * ·µ»ØÎļþ¼Ç¼ָÕëµÄµ±Ç°Î»Öà void seek(long pos) * ½«Îļþ¼Ç¼ָÕ붨λµ½posµÄλÖà long length() * »ñÈ¡ÎļþµÄ³¤¶È int read() * ¶Áȡһ¸ö×Ö½ÚµÄÊý¾Ý int read(byte b[]) * ¶ÁÈ¡Êý¾Ýµ½ byte[] readFully(byte b[]) int read(byte b[], int off, int len) * ¶ÁÈ¡Êý¾Ýµ½ byte[] * ´Óbyte[]µÄoff¿ªÊ¼Ð´Èë,дÈëlen³¤¶È readLine(); * ´ÓÖ¸ÕëλÖöÁÈ¡µ±Ç°ÐÐ,Ö»»á¶ÁÖ¸ÕëÕâÒ»ÐкóÃæµÄÊý¾Ý # ¶ÁÈ¡Demo RandomAccessFile randomAccessFile = new RandomAccessFile(PATH,"rw"); randomAccessFile.seek(60); //ÉèÖÃÖ¸ÕëλÖà int len = 0; byte[] buf = new byte[1024]; while ((len = randomAccessFile.read(buf)) != -1){ System.out.println(new String(buf,0,len,"GBK")); } randomAccessFile.close(); # дÈëDemo RandomAccessFile randomAccessFile = new RandomAccessFile(PATH,"rw"); randomAccessFile.seek(randomAccessFile.length()); //ÒÆµ½Ö¸ÕëµÄ×îºó randomAccessFile.write("//¹þ¹þ¹þ,Õâ¸öÊÇ×·¼ÓµÄÓ´".getBytes("GBK")); //дÈëÊý¾Ý # ¼àÌýÎļþµÄ±ä»¯ // ²»ÄÜ´ò¿ªÐ´È¨ÏÞ("w"),²»È»ÆäËû³ÌÐòû·¨Ð´ÈëÊý¾Ýµ½¸ÃÎļþ try(RandomAccessFile randomAccessFile = new RandomAccessFile("D:\\log.log", "r")){ // ×îºóÒ»´ÎÖ¸ÕëµÄλÖÃ,ĬÈÏ´ÓÍ·¿ªÊ¼ long lastPointer = 0; // ÿһÐжÁÈ¡µ½µÄÊý¾Ý String line = null; // ³ÖÐø¼àÌýÎļþ while(true) { // ´Ó×îºóÒ»´Î¶ÁÈ¡µ½µÄÊý¾Ý¿ªÊ¼¶ÁÈ¡ randomAccessFile.seek(lastPointer); // ¶ÁȡһÐÐ,Óöµ½»»ÐзûÍ£Ö¹,²»°üº¬»»Ðзû while((line = randomAccessFile.readLine()) != null) { System.out.print(line + "\n"); } // ¶ÁÈ¡Íê±Ïºó,¼Ç¼×îºóÒ»´Î¶ÁÈ¡µÄÖ¸ÕëλÖà lastPointer = randomAccessFile.getFilePointer(); } }