Before officially introducing how to use JAVA input/output-related classes for file access, you can simply use Java. io. randomaccessfile is used to access files, so as to understand the concepts and things that must be paid attention to during file access.
File Access is usually sequential. Every time a file is accessed, the file read location will move forward from the current location. However, sometimes you must specify a certain segment of the file for reading or writing, that is, random access, that is, you must be able to freely move the reading location in the file. In this case, you can use randomaccessfile and its seek () method to specify the file access location, in bytes.
To facilitate mobile access, the length of each data is usually fixed in the random access file. For example, if the length is fixed to the personal data of every student, there is no direct method in Java to write a fixed length data (such as the structure in C/C ++ ), therefore, you must design your own settings to fix each length. Example 14.2 first design a student data class.
Example 14.2 student. Java
Package onlyfun. Caterpillar;
Public class student {
Private string name;
Private int score;
Public student (){
Setname ("noname ");
}
Public student (string name, int score ){
Setname (name );
This. Score = score;
}
Public void setname (string name ){
Stringbuilder builder = NULL;
If (name! = NULL)
Builder = new stringbuilder (name );
Else
Builder = new stringbuilder (15 );
Builder. setlength (15); // a maximum of 15 characters
This. Name = builder. tostring ();
}
Public void setscore (INT score ){
This. Score = score;
}
Public String getname (){
Return name;
}
Public int getscore (){
Return score;
}
// Each data is invariably written into 34 bytes
Public static int size (){
Return 34;
}
}
For each instance of student data, when writing a file, it is fixed to 34 bytes in length, that is, 15 characters (30 bytes) plus the length of an int INTEGER (4 bytes ). In Example 14.2, stringbuilder is used to fix the character length. You can use the size () method to obtain the length information. Example 14.3 demonstrates how to use randomaccessfile to write a file and randomly specify the data you want to read.
Example 14.3 randomaccessfiledemo. Java
Package onlyfun. Caterpillar;
Import java. Io .*;
Import java. util .*;
Public class randomaccessfiledemo {
Public static void main (string [] ARGs ){
Student [] students = {
New student ("Justin", 90 ),
New student ("momor", 95 ),
New student ("Bush", 88 ),
New student ("caterpillar", 84 )};
Try {
File file = new file (ARGs [0]);
// Create a randomaccessfile instance and open the file in read/write mode
Randomaccessfile =
New randomaccessfile (file, "RW ");
For (INT I = 0; I <students. length; I ++ ){
// Write data using the corresponding write Method
Randomaccessfile. writechars (students [I]. getname ());
Randomaccessfile. writeint (students [I]. getscore ());
}
Required bytes = new bytes (system. In );
System. Out. Print ("what data is read? ");
Int num = bytes. nextint ();
// Use the seek () method to operate the access location
Randomaccessfile. Seek (num-1) * student. Size ());
Student = new student ();
// Read data using the corresponding read Method
Student. setname (readname (randomaccessfile ));
Student. setscore (randomaccessfile. readint ());
System. Out. println ("name:" + student. getname ());
System. Out. println ("score:" + student. getscore ());
// Set to close the file
Randomaccessfile. Close ();
}
Catch (arrayindexoutofboundsexception e ){
System. Out. println ("Please specify the file name ");
}
Catch (ioexception e ){
E. printstacktrace ();
}
}
Private Static string readname (randomaccessfile)
Throws ioexception {
Char [] Name = new char [15];
For (INT I = 0; I <name. length; I ++)
Name [I] = randomaccessfile. readchar ();
// Replace null characters with space characters and return
Return new string (name). Replace ('\ 0 ','');
}
}
Execution result:
Java onlyfun. Caterpillar. randomaccessfiledemo student. dat
What data is read? 2
Name: momor
Score: 95
The implementation of related methods on randomaccessfile is described in annotations. You can see several necessary processes for reading and writing files:
Open the file and specify the read/write mode in Java. When an input/output class related to the file is instantiated, the file will be opened. When instantiating a file, you must specify that the file is to be opened in read (R), write (W), or read/write (RW) mode. The file can be viewed as a container, to read or write data, you must open the container cap.
Use the corresponding Writing Method to write the file. In Java, the write name is usually used as the beginning. In low-level file writing, to write certain types of data, you must use methods of the expected type, such as writeint () and writechar.
Use the corresponding reading method to read the file. In Java, the read name is usually used as the beginning. In low-level file reading, to read data of a certain type, you must use methods of the desired type, such as readint () and readchar.
Closing a file can regard the file as a container. to read or write data, you must open the bottle cap of the container. If you do not read or write data, you must close the bottle cap. For some file access objects, the action to close the file means to write all the data in the buffer into the file. If you do not close the file, some data may be lost without being written to the file.
Recommended for beginners a programming technology learning site, 96 stack Software Programming Network, http://www.96dz.com, which has c ++ video tutorial, C # video tutorial, Java video tutorial download, c \ c ++, Java, and C #. net and other programming technology abstracts, including the current mainstream Linux programming and web programming learning materials video tutorial download.