File / file
java.io package
format:
File file = new File("路径");
path
-
Absolute path:
-
Looking down from the letter;
-
From the project root directory to start looking
-
-
relative path:
-
Edit the file from the current path relative to the path of the file you're looking for
../ upper level
-
Try occasions usually find in the project file
-
Common methods:
// Returns the parent directory path abstract .getparentFile (); // if the file exists .exists (); // create an empty file which will throw an exception. .CreateNewFile (); // delete .delete (); // get the path .getAbsolutepath (); // get the file name .getName (); // get the file's parent, returns String .getParent (); // determine whether the folder .isDirectory (); // get the current All file names in the path .list ();
In real life we often need to extract some files in a folder, if it is to get all the files in the judgment, this is very troublesome, java provide such a method for us
.list(FilenmeFilter in);
This is a list () method is overloaded, incoming FilenmeFilter an excuse, we can implement this interface by way of anonymous inner classes,
.list(new FilenmeFilter(){ public boolean accept((File file),(String name)){ if(name.endWith(".clsaa")) return ture; return false; } });
Classification stream
Classification | Class A | Class B |
---|---|---|
Transmission direction | Input stream | Output stream |
Number of types of data transmission | Byte stream | Jifuryu |
Operation method | Node Stream | Flow filtration |
Other: commutations
Byte stream - the input stream
Role: to read the file.
类名:FileInputSteam
format:
FIS = FileInputSteam new new FileInputSteam ( "file path" ); // 1.FileInputSteam ( "File Path") will throw an exception // 2. If the file is a relative path, the folder will only need to write the file name, the SRC next you need to add SRC folder. // the Java class at compile time will be automatically added to the package name.
method:
int n = fis.read (); // 1. reads a byte, integer return (for Chinese characters can not be read) and stored in the n @ 2 each use, a cursor backward @ -1 3. At the end the
int n = FIS ((. byte [] bytes)); // in bytes the length of the byte 1. The number of bytes read, store // valid data length n is 2. Return an array of bytes read // -1 3. At the end the
Byte stream - the output stream
Fos = FileOutputSteam new new FileOutputSteam ( "file path" ); // 1.FileOutputSteam ( "File Path") will throw an exception // 2. If the file is a relative path, the folder will only need to write the file name, the SRC next you need to add SRC folder. // the Java class at compile time will be automatically added to the package name. // 3. If the path of the last file does not exist, it will automatically be added, (but the middle of the path must exist, or will be error)
method:
fos.write (); // Use analog .read (), including overload .write ((byte [] bytes) );
Stream closed
-
Written finally in
-
First determine whether the air
-
Close .close function ();
-
Also throw an exception when closing the stream, remember to shut down a stream, a try / catch
-
Since the independence of the try / catch the language, so to flow statement in the try / catch statement outside
Jifuryu
The upper layer of abstraction
-
Reader / from operator input, common implementation class: FileReader
-
Writer / output from the symbol, common implementation class: FileWriter
Features: read a character - two bytes
Usage is similar to the byte stream
Flow filtration
Buffer flow
Class Name:
-
Byte Input: BufferedInputStream
-
Byte outputs: BufferedOutputStream
-
Character Input: BufferedReader
-
Character output: BufferedWriter
Constructor:
BufferedInputStream (InputStream in); // buffer is circulated into subclasses implement InputStream interface // while closing the stream, it is closed, which also closes the internal flow
characteristic:
-
Read several times faster
-
Without closing the buffer flow, part of the data will remain in the "buffer tube", resulting in missing data
Performs the closed flow .flush (); (Wash Buffer Tube Method)
-
There is a special character input method stream
.readline (); // 1. read a line of characters, not including newline (\ n-) // 2. read completion return null // 3. corresponding .write (..... + "\ n" ), you need to add "\ n"
Class Name: PrintWriter
method:
.println ((String str)); // Print and line feed