Java know how much (72) random Read and write files

Source: Internet
Author: User
Tags system log java throws

The Java.io package provides the Randomaccessfile class for creating and accessing random files. With this class, you can jump to and read data from anywhere in the file. A program can insert data into a random file without destroying other data for that file. In addition, the program can update or delete previously stored data without rewriting the entire file.

The Randomaccessfile class is a direct subclass of the object class and contains two main construction methods for creating Randomaccessfile objects, as shown in table 10-11.

Table 10-11 How to construct Randomaccessfile class
Construction Method function Description
Public Randomaccessfile (string name, string mode) Specifies the file name of the random file stream object, in mode to indicate the access mode to the file
Public randomaccessfile (file file, String mode) Specifies the file name of the random file stream object in file, with mode as the access mode


It is important to note that mode represents the operation state of the random read-write file that is created, and its value includes:

    • R: Indicates that the file is opened as read-only.
    • RW: Indicates that the file is opened read-write, using this mode to implement both read and write operations with only one object.


Table 10-12 lists the common methods and descriptions of the Randowaccessfile class.

Table 10-12 Common methods of Randowaccessfile
Method function Description
Long Length () Return file length
void Seek (Long POS) Move file position indicator, POS specifies the number of stray bytes from the beginning of the file
int skipbytes (int n) Skips n bytes and returns the number of bytes actually skipped
int read () Reads a byte from a file, the high 24 bits of bytes are 0, and if the end of the file is encountered, returns-1
Final byte readbyte () Reading signed byte values from a file
Final Char ReadChar () Read a Unicode character from a file
final void Writechar (inte c) Write one character, two bytes

1"Example 10-12"imitates the system log to write data to the end of the file. 2 //********** Ep10_12.java **********3 ImportJava.io.*;4 classep10_12{5      Public Static voidMain (String args[])throwsioexception{6         Try{7BufferedReader in=NewBufferedReader (NewInputStreamReader (system.in));8String s=in.readline ();9Randomaccessfile myfile=NewRandomaccessfile ("Ep10_12.log", "RW");TenMyfile.seek (Myfile.length ());//move to end of file OneMyfile.writebytes (s+ "\ n");//Write Data A myfile.close (); -         } -         Catch(IOException e) {} the     } -}

After the program runs, a Ep10_12.log file is created in the directory, and the content entered at each run is added at the end of the file's content.

Series Articles:Java know how much (top)Java know how much (interface) interfaceJava knows how much (40) the difference between an interface and an abstract classJava know how much (41) generic explanationJava know how much (42) the range of generic wildcard characters and type parametersJava know how much (43) Exception Handling BasicsJava know how much (44) exception typeJava know how much (45) uncaught ExceptionsHow much Java knows (the) use of try and catchJava know how much (47) use of multiple catch statementsJava knows how much (in) the nesting of try statementsJava know how much (a) Throw: Exception throwsJava know how many () Java throws clausesJava knows how many (or) finallyJava know how much (52) built-in exceptionsJava know how much (53) Use Java to create your own exception subclassesJava know how much (54) assertion detailedJava know how many (55) threadsJava know how much (56) threading ModelJava know how much (57) main threadJava know how much (58) thread Runnable interface and thread class explanationJava know how much (59) Create multithreadingJava know how much (isAlive) and join () useJava know how much (61) thread PriorityJava know how much (62) thread synchronizationJava know how much (63) inter-thread communicationJava know how much (64) thread deadlockJava know how much (65) thread hangs, resumes, and terminatesJava know how much (66) Overview of input and output (IO) and streamsJava know how much (67) character-oriented input streamJava know how much (68) character-oriented output streamJava know how much (69) byte-oriented input and output streamJava know how much (70) application for byte streamJava know how much (71) file and directory management

Java know how much (72) random 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.