Java base-io Stream object random Access file (randomaccessfile)

Source: Internet
Author: User

Java base-io Stream object random Access file (randomaccessfile)

Yun Zhengjie

Copyright Notice: Original works, declined reprint! Otherwise, the legal liability will be investigated.

I. Randomaccessfile INTRODUCTION

Instances of this class support read and write to random-access files. Random access to a file behaves like a large byte array stored in the file system. There is a cursor or index that points to the suppressed array, called a file pointer , and the input operation reads the bytes from the file pointer and moves the file pointer forward as the byte is read.

Two. Randomaccessfile Usage Show

1 /*2 @author: Yinzhengjie3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/4 Email:[email protected]5 */6 7  PackageCn.org.yinzhengjie.note6;8 9 ImportJava.io.RandomAccessFile;Ten  One /* A  *  - * Randomaccessfile (File file,string mode) - * Randomaccessfile (String name,string mode) the  */ -  -  -  Public classRandomaccessfiledemo { +      Public Static voidMain (string[] args)throwsException { -Randomaccessfile RAF =NewRandomaccessfile ("Yinzhengjie.txt", "RW"); +         //write Data of byte type ARaf.write (97);  at         //Print the current cursor position in the file - System.out.println (Raf.getfilepointer ());  -         //write data of type int -Raf.writeint (200); - System.out.println (Raf.getfilepointer ());  -         //data written to characters inRaf.writeutf ("Hello"); - System.out.println (Raf.getfilepointer ());  toRaf.writeutf ("World"); + System.out.println (Raf.getfilepointer ()); -          the         //positioning to the string offset, if it is a character, the position that needs to be moved is the length of the character.  *Raf.seek (12); $         //read a stringPanax NotoginsengString s =Raf.readutf (); - System.out.println (s); the System.out.println (Raf.getfilepointer ()); + raf.close (); A     } the } +  - /* $ The result of the above code execution is as follows: $ 1 - 5 -  A the  + -  WorldWuyi  + the */

Java base-io Stream object random Access file (randomaccessfile)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.