Randomaccessfiledemo1~2 how to read and write files

Source: Internet
Author: User
Tags prepare readable

randomaccessfiledemo1  ways to write files  /** *     file operations  --- randomaccessfile  *    java provides an operation that allows random access to a file, including read and write operations. Read and write file data based on pointer form.  *     This class is named Randomaccessfile. The read and write of this class is a pointer-based operation.       randomaccessfile supports two creation modes      read-only mode: only read file data work       read/write mode: read/write to file Data       common construction methods     randomaccessfile ( File file,string mode)                      //If there is a file object write Object     randomaccessfile (String  Path,string mode)                      //If there is no file object, write the path name directly      where mode corresponds to the string:     r: Read-only mode     RW: Read-write mode       read-write method:    &NBsp Randomaccessfile provides a way to write bytes to a file:  void write (int d)      Randomaccessfile provides a way to read bytes from a file:  int read ()      Note: You must close the release resources after you have read and write all finished    */
 Public classRandomAccessFileDemo1 { Public Static voidMain (string[] args)throwsIOException {//Create a file-access-based read-write mode randomaccessfile we only need to pass in "RW" in the second parameter. Randomaccessfile Rafdemo =NewRandomaccessfile ("RafDemo.txt", "RW"); //then the access to the file using Randomaccessfile is readable and writable.         /*Randomaccessfile provides a way to write bytes to a file: * void write (int d) * Writes out a byte that is written for the 2-decimal "low eight-bit" * (square) corresponding to the given int value The method writes a byte according to the position of the current pointer, which is the "low 8 bits" of the parameter int. ) * VVVVVVVV * 00000000 00000000 00000000 00000001*/Rafdemo.write (97); /*a binary low eight-bit conversion is made to the Rafdemo file. The text editor converts the low eight bits into characters. So the contents of RafDemo.txt are opened with Notepad and a character a is displayed.*/        /*Prompt Statement*/System.out.println ("Write Complete"); //release resources must be closed after read and write is completeRafdemo.close (); }}
View CodeRandomAccessFileDemo2 Read File Contents/** * File Operation---randomaccessfile * * Randomaccessfile provides a way to read bytes from a file: int read () *     The method reads a byte (8-bit) padding from the current pointer position of Randomaccessfile to the low eight bits of int, a height of 24 bits is 0, and a return value range positive: 0~255, if return-1 means that the end of the file is read to EOF (eof:end of files)!    Automatically moves the file pointer after each read to prepare for the next read.        Summary: 1. In the case of no parameter, no loop, this method only reads the contents of the first byte (8 bits) and returns the corresponding int type value.     2. If the content of this byte exceeds the 0~255 range, then only the lowest 8 bits of the binary of this int integer are intercepted. Note: The release resource must be closed after read and write is complete */
 Public classRandomAccessFileDemo2 { Public Static voidMain (string[] args)throwsIOException {//Create a file-access-based read-write mode randomaccessfile we only need to pass in "RW" in the second parameter. Randomaccessfile Rafdemo =NewRandomaccessfile ("RafDemo.txt", "RW"); //then the access to the file using Randomaccessfile is readable and writable.         /*read a byte method: int Read () The method reads a byte (8-bit) from the current pointer position of Randomaccessfile to the low eight bits of int, the height 24 bits to 0, and the return value range positive: 0~255,         If return-1 means that the end of the file is read EOF (eof:end of file)!         Automatically moves the file pointer after each read to prepare for the next read. */        intReturnlen =Rafdemo.read (); //Note: Read rule: Reads a data byte (8 bits) from the current pointer position in this file. //converts 1 bytes of content to decimal, returned to int returnlen.System.out.println (Returnlen); //release resources must be closed after read and write is completeRafdemo.close (); }}
View Code

Randomaccessfiledemo1~2 how to read and write files

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.