1.5 JAVA IO operations

1.5 JAVA IO operations

Reference link: https: //www.runoob.com/java/java-files-io.html

A, JAVA IO operations

Since JAVA external reference data, or to send their data to the outside world, then we need to use the input and output. JAVA is the IO.

Character stream typically text input console (Reader writer console data) (Writer output data to the console),

Byte stream usually writes to files ( the InputStream to read data from a file ) written ( the OutputStream  to write data to a file ).

1.1 reading console input

Reading console input parameters system.in

// Use BufferedReader to read characters in the console
 
import java.io. * ;
 
public class BRRead {
    public static void main(String args[]) throws IOException {
        char c;
        // 使用 System.in 创建 BufferedReader
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println ( "input character, press 'q' to quit." );
         // read characters 
        do {
            c = (char) br.read();
            System.out.println(c);
        } while (c != 'q');
    }
}

1.2 console output

By console output print () and println (). These methods defined by the class PrintStream, System.out is a reference to that object.

PrintStream inherited OutputStream class and implements the methods write (). write () methods are not often used because print () and println () method is more convenient to use.

1.3 write file (input stream)

InputStream is the parent class for all input stream of bytes, a data stream is defined as a sequence. For reading data from the input source stream, the output stream for writing data to a target.

FileInputStream the stream used to read data from a file, its object can be created with the new keyword.

// create two ways 
InputStream f = new new FileInputStream ( "C: / the Java / the Hello" );

File f = new File("C:/java/hello");
InputStream out = new FileInputStream(f);

InputStream is the parent of all the input byte stream, creating a method InputStream object can be called to read the stream flow or perform other operations. read () method is the foundation.

// close this file input stream and releases any system resources associated with this stream. Throws IOException. 
the out.close ();
 // This method clears the connection with the file. Ensure that no more references to call the close method when the file input stream. Throws IOException. 
out.finalize ();
 // This method reads the specified bytes of data from the InputStream object. It returns an integer value. 
out.read ( int R & lt);
 // read byte length r.length 
int Read ( byte [] R & lt)
 // next time this input method returns the invocation stream without blocking read from this input stream bytes 
Available ();

In addition InputStream, there are some other input stream ByteArrayInputStream, DataInputStream.

1.4 the output stream (a FileOutputStream)

OutputStream is the parent class for all byte output stream, write () is a basic grammar, the class used to create a file and write data files. This class is used to create a file and write data files. If the stream before the file is opened for output, the target file does not exist, then the flow will create the file.

// create two ways 
OutputStream f = new new FileOutputStream ( "C: / the Java / the Hello" )

File f = new File("C:/java/hello");
OutputStream f = new FileOutputStream(f);

After completion of OutputStream object, you can use Close (), Finalize (), Write (W int), Write (byte [] W).

Still other output streams ByteArrayOutputStream, the DataOutputStream

// File name: Example of IO operations 
Import java.io. * ;
 
public class fileStreamTest2 {
    public static void main(String[] args) throws IOException {
        File f = new File("a.txt");
        FOP FileOutputStream = new new FileOutputStream (F);
         // build FileOutputStream object, file does not exist is automatically created 
        OutputStreamWriter Writer = new new OutputStreamWriter (FOP, "UTF-. 8" );
         // build OutputStreamWriter objects, the encoding parameters can be specified, the default is the operation default encoding system, the windows are GBK 
        Writer.append ( "Chinese input" );
         // written to the buffer 
        Writer.append ( "\ R & lt \ n-" );
         // wrap 
        Writer.append ( "Dictionary Dictionary English" );
         / / cache is flushed red, written to a file, if the following has no written content, and also written directly to close 
        writer.Close ();
         // close the write stream, while the buffer will write the contents of files, the above comment out
        fop.close ();
         // close the output stream, freeing system resources 
        FileInputStream FIP = new new FileInputStream (F);
         // build the object FileInputStream 
        InputStreamReader Reader = new new InputStreamReader (FIP, "UTF-. 8" );
         // build InputStreamReader objects, writing the same encoding 
        the StringBuffer SB = new new the StringBuffer ();
         the while (reader.ready ()) {
            sb.append (( char ) reader.Read ());
             // switch to char added StringBuffer object 
        }
        System.out.println(sb.toString());
        reader.close();
        // Close the stream read 
        fip.close ();
         // close the input stream, free system resources 
    }
}

1.5 File and IO operations

Create folders both ways

mkdir () method to create a folder, returns true if successful, false if it fails. Failure indicates that File object specified path already exists, or because the entire path does not exist, the folder can not be created.

mkdirs () method to create a folder and all of its parent folder.

File d = new new File ( dirname ) ; // Now create a directory d . Mkdirs ( )

Determine whether there is a directory, isDirectory () method returns true.

Delete the directory,  DeleteFolder ( Folder )

 

 

  

Guess you like

Origin www.cnblogs.com/Smileing/p/11930194.html