1. File class
Java uses the file class (Java.io.File) to represent files or directories in the file system on the operating system. In other words, we can use file to manipulate files or directories on the hard disk to create or delete them.
File can describe the names of files or directories, size and other information, but not the contents of the operation of the file! The constructors for the file class are all arguments.
1) Description of the path: Different file system differences are large, Linux and windows are different! It is best to use relative paths, not absolute paths.
2) ". "Represents the path: the current directory (the directory where the project is located), under Eclipse_workspace/project_name, File.separator: Constants, directory separators, recommended use! Depending on what type of delimiter is automatically recognized by the system,/,linux is \ in Windows.
3) Creating the object does not imply that the file is on the corresponding path on the hard disk, but that the object is created in memory to represent the file specified by the path. Of course the file corresponding to this path may not exist at all!
4) CreateNewFile () has a throws declaration that requires a forced catch exception!
5) Create a new file or directory:
①boolean mkdir (): Directories can only be created based on an existing directory.
②boolean mkdirs (): All necessary parent directories (non-existent auto-created) are created and the directory is created.
③boolean CreateNewFile (): Creates an empty new file.
6) Two ways to create files in a directory:
① directly specifies where the file needs to be created and calls CreateNewFile (), provided that the directory exists!
② Create a file instance to specify the directory to be stored, if the directory does not exist, create all the nonexistent directories, and then create a file instance, representing the Data.dat files, created based on the last file instance representing the directory. Create a file instance using the file dir,string FileName construction method, and then call CreateNewFile (): Represents the files specified by the filename in the directory represented by Dir.
Case Study:
650) this.width=650; "style=" width:641px;height:315px; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/ M00/6f/4b/wkiom1wxgccgokqgaaf59sau8x4285.jpg "width=" 658 "height=" "alt=" wkiom1wxgccgokqgaaf59sau8x4285.jpg "/ >
7) Common methods for viewing file or directory properties
①long Length (): Returns the size of the file.
②long lastmodified (): Returns the last time the file was modified.
③string getName (): Returns the file or directory name. ⑧string GetPath (): Returns the path string.
④boolean exists (): exists. ⑨boolean Isfile (): Whether it is a standard file.
⑤boolean isdirectory (): Whether it is a directory. ⑩boolean CanRead (): Can be read.
⑥boolean canWrite (): Can write, modify.
⑦file[] Listfiles (): Gets the child of the parent directory (file or directory )
Case 18:
650) this.width=650; "style=" WIDTH:642PX;HEIGHT:507PX; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/ M01/6f/49/wkiol1wxgh7yyomyaajjq58yday672.jpg "width=" 660 "height=" 521 "alt=" wkiol1wxgh7yyomyaajjq58yday672.jpg "/ >
8) Delete a file: Boolean delete (): ① write the file name directly as the path and "./data.dat" for the same file, or you can write the directory name directly, but note the 2nd article. ② Delete a directory: To ensure that there are no subkeys in the directory before you can delete the directory, or delete failed!
Case 19:
650) this.width=650; "style=" WIDTH:627PX;HEIGHT:507PX; "title=" Clipboard.png "src=" http://s3.51cto.com /wyfs02/m02/6f/49/wkiol1wxglein026aajeqjqqrps491.jpg "width=" 662 "height=" 519 "alt=" Wkiol1wxglein026aajeqjqqrps491.jpg "/>
2. randomaccessfile Random Access file stream
It is easy to read and write the contents of a file, but only one byte (byte) reads and writes 8 bits.
1) The hard disk of the computer is byte-byte byte by byte when the data is saved.
2) Randomaccessfile Open file mode: RW: After opening the file can read and write operation; R: Read only after opening the file.
3) Randomaccessfile is a pointer-based read and write operation, where the pointer is read and written from.
①void Seek (Long pos) method: The next read and write operation occurs at this location from the beginning of the file to the pointer offset at the set position.
②getfilepointer () Method: Gets the current position of the pointer, and seek (0) moves the pointer to the beginning of the file.
③int skipbytes (int n) method: Attempts to skip the input n bytes.
4) The constructors of the Randomaccessfile class are all arguments.
①randomaccessfile constructor Method 1, parameter is file
Randomaccessfile raf=new randomaccessfile (file, "RW");
②randomaccessfile constructor Method 2, parameter is a string path (file name)
Randomaccessfile raf=new randomaccessfile ("Data.dat", "RW");
5) Read and write operation is finished, no longer write off: Close ();
6) Common methods
Writing method
①write (int data): Writes the first byte, and is written from a low 8-bit.
②write (byte[] data): Writes a set of bytes.
③write (byte[] data.int offset,int length): Writes from the offset position of the data array and writes the length of bytes to the file consecutively.
④writeint (int): writes 4 bytes at a time, writes an int value.
⑤writelong (Long): Write 8 bytes at a time, write a Long value.
⑥writeutf (String): Writes a string to the file consecutively in UTF-8 encoding.
Reading method:
①int Read (): reads a byte and returns 1 if it has been read to the end of the file.
②int Read (byte[] buf): Attempts to read buf.length bytes. And the bytes read are stored in the BUF array. The return value is the number of bytes actually read.
③int readInt (): reads 4 bytes continuously, returns the INT value
④long Readlong (): Reads 8 bytes continuously, returns the Long value
⑤string readUTF (): reads a string continuously from the file in UTF-8 encoding, returning the string value
Case 20:
650) this.width=650; "style=" width:625px;height:476px; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/ M00/6f/4b/wkiom1wxghvwnygjaapxe2cw8tg010.jpg "width=" 721 "height=" 551 "alt=" wkiom1wxghvwnygjaapxe2cw8tg010.jpg "/ >
7 Randomacceccfile performs a copy operation: reads a file and writes each byte of the file to another file to complete the copy function.
Case 21:
650) this.width=650; "style=" width:643px;height:503px; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/ M01/6f/49/wkiol1wxhdvb7kltaanh2g2eqdw936.jpg "width=" 665 "height=" 519 "alt=" wkiol1wxhdvb7kltaanh2g2eqdw936.jpg "/ >
This article is from "Forever Young" blog, please be sure to keep this source http://kingkongzhao.blog.51cto.com/6319491/1670902
Java Core API--(File, Randomaccessfile)