Java learning == Java Learning> IO file operating system ==> IO operating system files

 

I. Overview

The most important is that the entire Java.io in five classes and an interface. 5 refers to a class File, InputStream, OutputStream, Reader, Writer, refers to an interface Serializable. IO mastered these core operating it for Java in the IO system and will have a preliminary understanding of.

  • File (file types): mainly used to describe attributes of a file or directory, for example: file size, modify the file name, delete the file, where the file path, etc. is determined.
  • The InputStream (input stream of bytes): abstract class, based on the input operation byte is the parent class for all input streams. Defines the common feature of all input streams have.
  • The OutputStream (output stream of bytes): abstract class. Byte output operation based. It is the parent class of all output streams. It defines a common feature of all the output streams have.
  • Reader (input stream): abstract class, a character based on the input operation.
  • Writer (output stream): abstract class, based on the operation of the output character.
  • Serializable():

Two, File class

By definition, File class is a direct subclass of Object, and it inherits the Comparable interface can sort the array. Operation of the File class includes creating files, delete, rename, get the path, when it was created, the following are common file manipulation functions.

 

File type is a file system file and folder objects packaged, can be operated by the file and folder objects thought. File class various metadata information save file or directory, including file name, file size, last modification time, whether read, obtain the path name of the current file, determines whether the specified file exists, obtain a list of files in the current directory, create , delete files and directories and other methods.

  File attribute manipulation
  Exercise 1
  English II

Third, the classification IO streams

1, the concept and flow action

  • Output data stream representative of any data source object capable, or capable of receiving the object data receiving side.
  • Nature stream data transmission, data transmission characteristics of the various types of flow abstract, convenient data manipulation more intuitive.
  • Effect is to create a flow path for conveying the data source and destination.

  It referred to in the abstract Java input and output streams, like pipes, to connect two containers. Flow is a set of sequential, starting and ending with a collection of bytes, it is the general term for data transmission or abstract. That data transmission between two devices called a stream.

2, classification IO streams

The flow of data divided into: the input and output streams.

  • Input stream: from external media (disk, network) -> memory;
  • Output streams: from memory -> external medium (disk, network);

The process is divided into different data types: a character stream and byte stream.

  • Byte stream: reading binary data, images, video, executable files and the like;
  • Character stream: generally used to read text files;

Four, IO stream architecture

 

As shown above, the article begins, as we mentioned, the most important of the five classes of Java IO system, in addition to the File class, the entire IO system nearly 50 categories, are from the other four classes (InputStream, OutputStream, Reader , Writer) derived. But these four classes are abstract classes, most of them need a method to achieve through their subclasses.

1, the stream file operations

 

These four classes are specialized file stream operation, the use of highly similar, except that the first two bytes is an operation flow followed by a two character stream operations. They will directly file stream operation, direct interaction with the underlying OS. Therefore they are also called nodes in the stream . Note the use of these objects stream after stream objects need to be closed, because the java garbage collector does not reclaim the initiative.

The following shows the four basic usage of the stream object:

  FileInputStream
  FileOutputStream
  FileReader
  FileWriter

2, the operational flow buffer

Computer access to external devices is very time-consuming. The higher the frequency of access, resulting in greater probability CPU idle. In order to reduce the number of external memory is accessed, one should access to the external device, to read and write more data. For this purpose, in addition to streaming nodes exchange between programs and data necessary to read and write mechanism, it should also increase the buffer mechanism. Buffer stream for each data stream is assigned a buffer, a buffer is a memory for temporarily storing data. This can reduce the number of hard disk access, improve transmission efficiency. 

  • BufferedInputStream: when data is written to the stream buffer when data is written to the buffer until the buffer is full, the system one time to send data to the output device;
  • BufferedOutputStream: When reading data, the system buffer to start reading data from the stream buffer until the buffer is empty, the system then reads the data from the input device into a buffer;
  • BufferedReader / BufferedWriter is a stream of characters (Reader) packed into the buffer flow;
  • A BufferedReader the readLine () can be easily read line, and a read only FileReader FileInputStream and a byte or character;

Similarly, we show you the basic usage:

  BufferedReader
  BufferedWriter
  BufferedInputStream
  BufferedOutputStream

3, summary

The basic operation of the process stream:

  • Given to the subject to be operated (File);
  • Establishing specific operation flow type (InputStream / OutputStream / Writer / Reader specific implementation class);
  • File operations, the read / write;
  • Close stream;

in conclusion:

  • Binary operation, such as: video, audio files, using only a byte stream, a character stream text file;
  • buffer is buffering operation, the case where no special requirements buffer can be used, we recommended buffer, more efficient;

Fifth, read the file properties

In Java there is a more important class Properties (Java.util.Properties), Java is mainly used to read configuration files, a variety of languages ​​are supported by their own configuration files, configuration files, many variables are constantly changing, so do also for the convenience of users, allows users to modify the program itself can be detached related variable settings. In Java, its configuration file often .properties file, a text file format, the format of the contents of the file format is "key = value", text annotations can be annotated with "#." The following look at its use:

Copy the code
PropDemo class {public 

  @Test 
  public void demo1 () throws IOException { 

    the Properties prop = new new the Properties (); 

    // here is the need to be an absolute path, otherwise it will error 
    // File propFile = new File ( " E: \\ Java_Project \\ config.properties resources \\ \\ MyProject "); 
    // prop.load (new new FileInputStream (propFile)); 

    // use ClassLoader to read files in the resources directory, you can write path relative path 
    prop.load ( . PropDemo.class.getClassLoader () the getResourceAsStream ( "the config.properties")); 

    // GET: returns the object 
    // getProperty: returns a string 
    Object = prop.get Driver ( "Driver"); 
    string URL = prop.getProperty ( "URL"); 

    System.out.println (URL); 
    System.out.println (Driver); 
  }
  
  @Test
  public void demo02() throws IOException {

    Properties prop = new Properties();

    // 使用ClassLoader来读取
    prop.load(PropDemo.class.getClassLoader().getResourceAsStream("config.properties"));

    prop.setProperty("name", "jim");

    String property1 = prop.getProperty("name");

    String property2 = prop.getProperty("YY", "abc");

    System.out.println(property1);
    System.out.println(property2);
  }
}
Copy the code

Configuration file as follows:

 

 
 
 

 

 

 

I. Overview

The most important is that the entire Java.io in five classes and an interface. 5 refers to a class File, InputStream, OutputStream, Reader, Writer, refers to an interface Serializable. IO mastered these core operating it for Java in the IO system and will have a preliminary understanding of.

  • File (file types): mainly used to describe attributes of a file or directory, for example: file size, modify the file name, delete the file, where the file path, etc. is determined.
  • The InputStream (input stream of bytes): abstract class, based on the input operation byte is the parent class for all input streams. Defines the common feature of all input streams have.
  • The OutputStream (output stream of bytes): abstract class. Byte output operation based. It is the parent class of all output streams. It defines a common feature of all the output streams have.
  • Reader (input stream): abstract class, a character based on the input operation.
  • Writer (output stream): abstract class, based on the operation of the output character.
  • Serializable():

Two, File class

By definition, File class is a direct subclass of Object, and it inherits the Comparable interface can sort the array. Operation of the File class includes creating files, delete, rename, get the path, when it was created, the following are common file manipulation functions.

 

File type is a file system file and folder objects packaged, can be operated by the file and folder objects thought. File class various metadata information save file or directory, including file name, file size, last modification time, whether read, obtain the path name of the current file, determines whether the specified file exists, obtain a list of files in the current directory, create , delete files and directories and other methods.

  File attribute manipulation
  Exercise 1
  English II

Third, the classification IO streams

1, the concept and flow action

  • Output data stream representative of any data source object capable, or capable of receiving the object data receiving side.
  • Nature stream data transmission, data transmission characteristics of the various types of flow abstract, convenient data manipulation more intuitive.
  • Effect is to create a flow path for conveying the data source and destination.

  It referred to in the abstract Java input and output streams, like pipes, to connect two containers. Flow is a set of sequential, starting and ending with a collection of bytes, it is the general term for data transmission or abstract. That data transmission between two devices called a stream.

2, classification IO streams

The flow of data divided into: the input and output streams.

  • Input stream: from external media (disk, network) -> memory;
  • Output streams: from memory -> external medium (disk, network);

The process is divided into different data types: a character stream and byte stream.

  • Byte stream: reading binary data, images, video, executable files and the like;
  • Character stream: generally used to read text files;

Four, IO stream architecture

 

As shown above, the article begins, as we mentioned, the most important of the five classes of Java IO system, in addition to the File class, the entire IO system nearly 50 categories, are from the other four classes (InputStream, OutputStream, Reader , Writer) derived. But these four classes are abstract classes, most of them need a method to achieve through their subclasses.

1, the stream file operations

 

These four classes are specialized file stream operation, the use of highly similar, except that the first two bytes is an operation flow followed by a two character stream operations. They will directly file stream operation, direct interaction with the underlying OS. Therefore they are also called nodes in the stream . Note the use of these objects stream after stream objects need to be closed, because the java garbage collector does not reclaim the initiative.

The following shows the four basic usage of the stream object:

  FileInputStream
  FileOutputStream
  FileReader
  FileWriter

2, the operational flow buffer

Computer access to external devices is very time-consuming. The higher the frequency of access, resulting in greater probability CPU idle. In order to reduce the number of external memory is accessed, one should access to the external device, to read and write more data. For this purpose, in addition to streaming nodes exchange between programs and data necessary to read and write mechanism, it should also increase the buffer mechanism. Buffer stream for each data stream is assigned a buffer, a buffer is a memory for temporarily storing data. This can reduce the number of hard disk access, improve transmission efficiency. 

  • BufferedInputStream: when data is written to the stream buffer when data is written to the buffer until the buffer is full, the system one time to send data to the output device;
  • BufferedOutputStream: When reading data, the system buffer to start reading data from the stream buffer until the buffer is empty, the system then reads the data from the input device into a buffer;
  • BufferedReader / BufferedWriter is a stream of characters (Reader) packed into the buffer flow;
  • A BufferedReader the readLine () can be easily read line, and a read only FileReader FileInputStream and a byte or character;

Similarly, we show you the basic usage:

  BufferedReader
  BufferedWriter
  BufferedInputStream
  BufferedOutputStream

3, summary

The basic operation of the process stream:

  • Given to the subject to be operated (File);
  • Establishing specific operation flow type (InputStream / OutputStream / Writer / Reader specific implementation class);
  • File operations, the read / write;
  • Close stream;

in conclusion:

  • Binary operation, such as: video, audio files, using only a byte stream, a character stream text file;
  • buffer is buffering operation, the case where no special requirements buffer can be used, we recommended buffer, more efficient;

Fifth, read the file properties

In Java there is a more important class Properties (Java.util.Properties), Java is mainly used to read configuration files, a variety of languages ​​are supported by their own configuration files, configuration files, many variables are constantly changing, so do also for the convenience of users, allows users to modify the program itself can be detached related variable settings. In Java, its configuration file often .properties file, a text file format, the format of the contents of the file format is "key = value", text annotations can be annotated with "#." The following look at its use:

Copy the code
PropDemo class {public 

  @Test 
  public void demo1 () throws IOException { 

    the Properties prop = new new the Properties (); 

    // here is the need to be an absolute path, otherwise it will error 
    // File propFile = new File ( " E: \\ Java_Project \\ config.properties resources \\ \\ MyProject "); 
    // prop.load (new new FileInputStream (propFile)); 

    // use ClassLoader to read files in the resources directory, you can write path relative path 
    prop.load ( . PropDemo.class.getClassLoader () the getResourceAsStream ( "the config.properties")); 

    // GET: returns the object 
    // getProperty: returns a string 
    Object = prop.get Driver ( "Driver"); 
    string URL = prop.getProperty ( "URL"); 

    System.out.println (URL); 
    System.out.println (Driver); 
  } 
  
  @Test
  public void demo02() throws IOException {

    Properties prop = new Properties();

    // 使用ClassLoader来读取
    prop.load(PropDemo.class.getClassLoader().getResourceAsStream("config.properties"));

    prop.setProperty("name", "jim");

    String property1 = prop.getProperty("name");

    String property2 = prop.getProperty("YY", "abc");

    System.out.println(property1);
    System.out.println(property2);
  }
}
Copy the code

Configuration file as follows:

 

Guess you like

Origin www.cnblogs.com/sdfdsg9912/p/11717154.html