A, File class
File class is the only class java.io package for file itself operates. It can create, delete files and other operations.
File type common operations
(1) Create a file
You can create a new file using createNewFille ().
Note:
Windows use the backslash representing the separator directory "."
Linux use a forward slash represents the directory separator "/."
The best practice is to use a static File.separator constant, the operating system may be selected according to the location corresponding to the delimiter.
(2) delete files
You can use delete () deletes a file.
Former general delete files, it is best to use exists () judgment about whether a file exists.
(3) Create a folder
You can use mkdir () to create the folder, but if you want to create parent path of the directory does not exist, it can not create success.
To solve this problem, you can use mkdirs (), when the parent path does not exist, it would have created together, along with the parent directory.
(4) lists all the files in the specified directory
File given in two ways List Folder Contents:
List (): List All names, returns an array of strings.
listFiles (): lists the full path and returns a File object array.
(5) delete the directory
You can use delete () to delete the directory.
Note that, if the directory is not empty directly with the delete () Deletes fail.
Second, understanding RandomAccessFile class
RandomAccessFile is access to the file content provided by Java, only to read files, write files can;
RandomAccessFile support random access files can be accessed anywhere in the file.
Basic Usage
(1) open a file, there are two open mode ----- "rw" (read-write), "r" (read only)
a RandomAccessFile Raf a RandomAccessFile new new = (File, "RW");
a RandomAccessFile capable of random access file content is because there is a pointer, the recording position read or write, the file is open = 0 pointer;
(2) the method of writing
write method can only write a byte, the write pointer points to the next location
raf. write (int); // 8 bits Xietu int is the
(3) method of reading
the same read method is to read one byte
int raf.read B = ();
(. 4) after the completion of reading and writing to files Close
raf.close ();
Third, the character stream with a byte stream
Operating procedures
1. Open a file File class
2. byte stream or a character stream subclass. Specify the location of the output.
3. The read / write
4. Close Input / Output
Byte stream and character stream
In operation java.io package file contents are two major categories: byte stream character stream. Is divided into two categories of input and output operation, the output data in the byte stream mainly OutputStream completed, the InputStream inputs, the output character stream primarily using the red-based Writer completed, completion of input mainly Reader
Byte stream
Byte stream type data byte main operation, whichever byte array, and OutputStream classes are mainly operating InputSream
output stream of bytes: OutputStream
input stream of bytes: InputStream
InputStream
Common method
Public int available (): get the size of the input file
Public void close (): close the input stream
Public int read (bytr [] b ): read the contents of the array, and returns the number of read
InputStream is an abstract class. To instantiate the need to use a subclass FileInputStream;
the InputStream Output = null;
the Input = new new FileInputStream (F);
OutputStream
Common method
Public void close (): close the output stream
Public void flush (): Flushes the buffer
Public void write (byte [] b ): speaking a byte array write data stream
Public void write (byte [] b , int off, int len): the specified byte array to write a data stream
Jifuryu
Character-output stream: Writer
character-input stream: Reader
Writer
Which itself is an abstract class in order to achieve FileWriter using
conventional methods
Public abstract void close (): close the output stream
Public void writer (String str): output string
Reader
Which itself is an abstract class in order to achieve FileReader using
conventional methods
Public abstract void close (): close the output stream
Public int read (): Read a single character
Public int read (char [] buf ): read the contents of which character set, returns the read length