1, File class
1.1. Overview and construction method of File class
File: it is an abstract representation of file and directory pathnames
Files and directories can be encapsulated into objects through File
File (String pathname) / / creates a new file instance by converting the given path name string to an abstract path name
File (String parent,String child) / / create a new file instance from the parent pathname string and child pathname string
File(File parent,String child) / / create a new file instance from the parent abstract pathname and child pathname strings
1.2. File class creation function
public boolean createNewFile() when the file with this name does not exist, create a new empty file named by the abstract path name. If the file does not exist, create the file and return true; If the file exists, return false;
public boolean mkdir() creates a directory named after this abstract pathname
public boolean mkdirs() creates a directory named by this abstract pathname, including any required but nonexistent parent directory
1.3 File class judgment and acquisition function
public boolean isDirectory() / / test whether the File represented by the abstract pathname is a directory
public boolean isFile() / / test whether the File represented by this abstract pathname is a File
public boolean exists() / / test whether the File represented by this abstract pathname exists
public String getAbsolutePath() / / returns the absolute pathname string of this abstract pathname
public String getPath() converts this abstract pathname to a pathname string
public String getName() returns the name of the file or directory represented by this abstract pathname
public String[] list() returns a string array of names of files and directories in the directory represented by this abstract pathname
public File[] listFiles() returns the File object array of files and directories in the directory represented by this abstract pathname
1.4. File class deletion function
public boolean delete() / / delete the file or directory represented by this abstract pathname
Absolute path: the complete path name. You can locate the file it represents without any other information
Relative path: it must be interpreted with information from other path names
1.5 recursion
Recursive exit: otherwise, memory overflow will occur
Recursive rule: a smaller problem similar to the original problem
Case: recursive factorial
public static int fun(int n) { if(n == 1) return 1; else return n*fun(n-1); } int n = fun(5);//Find the factorial of 5 System.out.println(n);
Case: traversing the directory
import java.io.File; import java.io.IOException; public class helloworld { public static void main(String[] args) throws IOException{ //Traverse directory File file = new File("E:"); getAllFilePath(file); } //Define a method to get all the contents in a given directory public static void getAllFilePath(File file) { //Gets the File array of all files or directories in the given File directory File[] filearray = file.listFiles(); //Traverse the file array to get each file object if(filearray != null) { for(File f : filearray) { //Determine whether the f object is a directory if(f.isDirectory()) { //yes getAllFilePath(f); } else{ //No, get the absolute path output to the console System.out.println(f.getAbsolutePath()); } } } } }
2, Byte stream
Byte input stream; Byte output stream
Character input stream; Character output stream
2.1 byte stream write data
Byte stream abstract base class
InputStream: this abstract class is a superclass that represents all classes of byte input stream
OutputStream: this abstract class is a superclass that represents all classes of byte output stream
Characteristics of subclass Name: subclass names are suffixed with their parent class name
FileOutputStream(String name): creates a file output stream and writes it to the file with the specified name
//Finally, all IO related operations should start to release resources
void close();// Close this file output stream and free up any system resources associated with this stream
2.2. Three ways to write data in byte stream
void write(int b) / / write the specified bytes to the file output stream, one byte at a time
void write(byte[] b) / / write b.length bytes from the specified byte array to the file output stream, one byte array data at a time
void write(byte[] b,int off,int len) / / write len bytes from the specified byte array to the output stream of this file from the offset off, and write part of the data of the byte array one at a time
2.3 two small problems of byte stream writing data
Line feed: different systems meet different requirements for line feed
window:\r\n
linux: \n
mac:\r
Additional content:
FileOutputStream(String name, boolean append) creates a file output stream and writes the file with the specified name; If the second parameter is true, the bytes are written to the end of the file instead of the beginning
2.4. Byte stream write data plus exception handling
Finally: provide a finally block to perform all cleanup operations during exception handling
2.5 byte stream read data (read one byte data at a time)
FileInputStream: get input bytes from files in the file system
FileInputStream(String name): create a FileInputStream by opening the connection with the actual file, which is named by the pathname in the file system
int read(); // The input stream reads one byte of data
Requirements: File FOS Txt is read and processed and output on the console
FileInputStream fis = new FileInputStream("fos.txt"); int by; while((by = fis.read()) != -1) { System.out.println((char)by); }
2.6 byte stream read data (read one byte array data at a time)
FileInputStream fis = new FileInputStream("fos.txt"); byte[] by = new byte[1024];//1024 and its integer multiples int len; while((len = fis.read(by)) != -1) { System.out.print(new String(by,0,len)); } fis.close();
Case: copying pictures
//Put 123 under E:java Jpg copy to current directory FileInputStream fis = new FileInputStream("E:\\java\\123.jpg"); FileOutputStream fos = new FileOutputStream("123.jpg"); byte[] by = new byte[1024];//1024 and its integer multiples int len; while((len = fis.read(by)) != -1) { fos.write(by,0,len); } fis.close(); fos.close();