Java input and output and file reading and writing (1)

                        java input and output and file reading and writing

According to the transmission direction of the data, the stream can be unprocessed raw binary data, or it can be a certain format after a certain encoding process.

Specific data, that is, a stream, is a sequence composed of a combination of bits or characters, such as a sequence of character streams and a sequence of digital streams.

A stream that transfers data from peripherals or external memory (such as keyboard, mouse, files) to an application is called an input stream; a stream that transfers data from an application to external memory or peripherals (screen, printer) Streams are called output streams. That is: the input stream reads data (here, the application reads data), and the output stream writes data (here, the application writes data).

Features of streaming input and output:

1. The acquisition and transmission of data are carried out along the sequence of the data sequence. Each data must wait for the data in front of it to be read or sent before it can be read or written. Each read and write operation processes the remaining data in the sequence. The first one that has not read or written data, but cannot choose the position of input and output.

2. Using data flow to process input and output makes the input and output operations of the application program independent of related devices, and each application program can be used for multiple input and output devices without any modification to the source code. Enhanced program portability.

Input and output streams are divided into: byte stream (bate stream) and character stream (character stream) according to the type of data processed. The byte stream reads and writes 8-bit binary numbers at a time. Since it can only read and write data in binary, but cannot decompose, reorganize and understand the data, it can be transformed and restored to its original meaningful state. Therefore, the byte stream is called a binary byte stream or bit stream. A character stream, on the other hand, reads and writes 16-bit binary numbers at a time and treats it as a character rather than a binary bit.

Byte stream: process byte data (basic types are InputStream, OutputStream)

Character stream: process character data (basic types are Reader, Writer)

File class: File, used to manage disk files and folders. That is: File is concerned with file storage, such as the last modification time of the file, storage location, etc., while Stream is concerned with file content. This is the difference between the two.

Example 1: Create a text file file.txt in the program, write a string of characters input from the keyboard, and then read the file and display the contents of the text file on the screen.

//filename:FileTest.java
import java.io. *;
class FileTest{
  public static void main(String[] args){
     char ch;
     int data;
     try{
           FileInputStream fis = new FileInputStream(FileDescriptor.in);//Declare and create a FileInputStream type object, and initialize the object for keyboard input
           FileOutputStream fos = new FileOutputStream("d:\\myfile.txt");//Declare and create a FileOutputStream type object, initialize the object
           System.out.println("Please enter a string ending with #: ");
           while((ch=(char)fis.read()) != '#')//Force type to char and judge
               fos.write(ch);
           fin.close();
           fos.close();
           FileInputStream fi = new FileInputStream("d:\\myfile.txt");//Declare and create a FileInputStream type object, initialize the object
           FileOutStream fo = new FileOutputStream(FileDescriptor.out);//Declare and create a FileOutputStream type object, initialize the object
           while(fi.avaliable() > 0)
          {
              data = fi.read();
              fo.write(data);
          }
          fi.close();
          fo.close();
     }
     catch(FileNotFoundException e){
          System.out.println("File not found");       
     }
  }
}

 Example 2: Copying binary image files with FileInputStream and FileOutputStream

//filename:ImageFileTest.java
import java.io. *;
public Class ImageFileTest{
    public static void main(String[] args) throws IOException{
        FileInputStream fi = new FileInputStream("d:\\scenery.jpg);//Declare and create an object of type FileInputStream, initialize the object
        FileOutputStream fo = new FileOutputStream("d:\\copyScenery.jpg");//Declare and create an object of type FileOutputStream, initialize the object
        byte [] b = new byte[fi.available()];//Create an array of byte type
        fi.read(b);//Read the graphics file into the b array
        fo.write(b);//Write the data of the b array to a new file 'copyScenery.jpg'
        System.out.println("The file has been copied and renamed");
        fi.close();
        fo.close();        
    }
}

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326762421&siteId=291194637