1. The File type

Java file classes represent file names and directory pathnames in an abstract way. This class is used to create files and directories, search for files, and delete files. The File object represents the files and directories that actually exist on the disk.

1. The main constructors of File are:

File(File parent,String child)
Copy the code

Creates a new File instance with the given abstract parent pathname and child path string

File(String pathname)
Copy the code

Create a new File instance by converting the given path string to an abstract path

File(String parent,String child)
Copy the code

Create a new instance of File with parent pathname string and child pathname string.

public String getName(a)Returns the name of the file or directory represented by this abstract pathnamepublic String getParent(a)Returns the pathname string of the parent pathname of this abstract pathname, or if the pathname does not specify a parent directorynull
public File getParentFile(a)Returns the abstract pathname of the parent pathname of this abstract pathname, or if the pathname does not specify a parent directorynull
public String getPath(a)Convert this abstract pathname to a path name stringpublic boolean isAbsolute(a)Tests whether this abstract pathname is an absolute pathnamepublic String getAbsolutePath(a)Returns an absolute pathname string for the abstract pathnameCopy the code

Note :File provides the ability to manipulate the contents of a File. There is no way to manipulate the contents of a File. If you want to manipulate the contents of a File, you also need a Stream.

— — — — — 2. IO flow Stream

A stream can be thought of as a sequence of data. The input stream represents reading data from a source and the output stream represents writing data to a target. Java provides powerful and flexible support for I/O, making it more widely used in file transfer and network programming.

1. Traffic classification

In terms of flow:

Input stream Output stream

According to the operation unit:

Byte stream: Universal stream: Anything can be converted to bytes, can transfer text, pictures, audio… Character stream: Can only manipulate plain text data

By function:

Node flow: encapsulates the source and realizes basic functions function flow: enhances the performance and efficiency of node flow

2. The byte stream

The byte stream is divided into byte input stream and byte output stream.

  • Byte InputStream :InputStream——> abstract class this abstract class is a superclass of all the classes that represent a byte InputStream
  • Byte OutputStream :OutputStream——> abstract class this abstract class is a superclass of all classes that represent a byte OutputStream
  • File byte input stream :FileInputStream—–> Gets input bytes from a file in the file system
  • File byte output stream :FileOutputStream—–> The byte stream in the output program is finished and should be closed. When a close() method can be used to write, the last call to flush() should flush out the remaining data in the stream pipe.

The following uses a replication operation as an example. There should be several steps for a copy operation:

To prepare a stream, declare and instantiate a file character stream object

Read and write data, using read(),write() method to achieve reading and writing

Brush out, and finally brush out the data in the pipe

Close the stream, noting that it should be closed first

Example 1:

    public class Test{
        public static void main(String []args){
            String src="E:/skr.txt";/ / the source file
            String dest="E:/skr1.txt";// Copy the file
            byteCopy(src,dest);
        }
        // Method overload
        public static void byteCopy(String src,String dest){
	        byteCopy(new File(src),new File(dest));
        }
        // Method overload
        public static void byteCopy(File input,File output) {
        	InputStream in = null;// Enter the byte stream declaration
        	OutputStream os = null;// Outputs the byte stream declaration
        	try {
	        	in = new FileInputStream(input);/ / polymorphism
	        	os = new FileOutputStream(output);/ / polymorphism
	        	// Define a 1K byte array to read 1K data at a time
	        	byte[] b = new byte[1024];
	        	// Returns -1 when there is no data in the file
	        	int len=-1;
	        	while(-1! =(len=in.read(b))){/ / read
		        	os.write(b,0,len);/ / write
	        	}
	        	os.flush();Brush the / /
            } catch (FileNotFoundException e) {
		        e.printStackTrace();
	        } catch (IOException e) {
	        	e.printStackTrace();
	        } finally {
	            // The stream that opens last closes first
	        	try {
		        	if(os ! =null) {
			        	os.close();
		        	}
		        	if(in ! =null) { in.close(); }}catch(IOException e) { e.printStackTrace(); }}}}Copy the code

3. The characters of the flow

Character stream is also divided into character input stream and character output stream

Character input stream :Reader– > Abstract class to read character stream

FileReader is a handy class for reading character files

Character output stream :Writer– > Abstract class to write to character stream

FileReader is a handy class for reading character files

Character streams can only operate on plain text files.

4. The buffer flow

Buffered flow is a function flow that enhances functions, improves performance, and speeds up read and write efficiency.

1. Byte buffer stream:

  • BufferedInputStream Byte Input stream buffer stream

  • BufferedOutputStream Byte Output stream buffer stream

There are no new methods, and polymorphic use can occur

2. Character buffer stream

BufferedReader character input streamString readLine(a)BufferedWriter (); BufferedWriter ();void newLine(a)Writes a line separatorCopy the code

Copy the code

3. The instantiation of a buffered stream is different from that of a normal node stream:

Take a look at the constructor using a byte input buffer stream as an example: BufferedInputStream(InputStream in) copy the code and you can see that in the constructor we’re passing in an object that’s a byte InputStream so instead of an abstract pathname of a file a string or an abstract pathname.

3. Summary

We can manipulate the outside of a File using methods provided in the File class, but when we want to manipulate the contents of a File, we need to use Stream. There are a number of flexible operations available in Java to process files. We can choose between byte streams or character streams to process files. When a file is large, you can also use buffer flow to improve performance and read/write efficiency.