The java.io package contains almost all the required classes for operation input and output. All of these flow classes represent the input source and output destination.
The streams in the Java.io package support many formats, such as basic types, objects, localized character sets, and so on.
A stream can be understood as a sequence of data. The input stream represents reading data from one source, and the output stream represents writing data to a target.
Java provides powerful and flexible support for I/O, making it more widely available in file transfer and network programming.
- Node Flow: You can read and write data from or to a specific place (node). such as FileReader.
- Processing flow: Is the connection and encapsulation of an existing stream, which implements the data read and write through the functional invocation of the encapsulated stream. such as BufferedReader. The construction method of the processing stream always takes an additional stream object to do the argument. A Stream object passes through multiple wrappers of other streams, called links to streams.
Console input and output:
Version after JDK 5 We can also use the Java Scanner class to get input from the console.
Read and write files:FileInputStream
Method 1: You can use the file name of the string type to create an input stream object to read the file
New FileInputStream ("C:/java/hello");
Method 2: You can also use a file object to create an input stream object to read the file. We first have to use the file () method to create a Document object:
New File ("C:/java/hello"new FileInputStream (f);
FileOutputStream
Method 1: Create an output stream object by using the file name of the string type:
New FileOutputStream ("C:/java/hello")
Method 2: You can also use a file object to create an output stream to write the file. We first have to use the file () method to create a Document object:
New New FileOutputStream (f);
//file name: Filestreamtest2.javaImportJava.io.*; Public classfilestreamtest2{ Public Static voidMain (string[] args)throwsIOException {File f=NewFile ("A.txt"); FileOutputStream FOP=NewFileOutputStream (f); //build FileOutputStream object, file does not exist automatically newOutputStreamWriter writer=NewOutputStreamWriter (FOP, "UTF-8"); //Build the OutputStreamWriter object, parameters can specify the encoding, default is the operating system default encoding, Windows is GBKWriter.append ("Chinese input"); //Write to BufferWriter.append ("\ r \ n"); //line BreakWriter.append ("中文版"); //Flush Cache Punch, write to file, if no content is written below, direct close also writesWriter.close (); //The write stream is closed and the buffer contents are written to the file, so the comment aboveFop.close (); //turn off the output stream and release system resourcesFileInputStream FIP=NewFileInputStream (f); //Building FileInputStream ObjectsInputStreamReader Reader=NewInputStreamReader (FIP, "UTF-8"); //build the InputStreamReader object with the same encoding as the writeStringBuffer SB=NewStringBuffer (); while(Reader.ready ()) {Sb.append (Char) Reader.read ()); //go to char and add to StringBuffer object} System.out.println (Sb.tostring ()); Reader.close (); //turn off Read streamFip.close (); //close the input stream and release system resources }}
file and I/O
There are also classes about files and I/O, and we need to know:
- File Class (Class)
- FileReader Class (Class)
- FileWriter Class (Class)
Directories in Java
To Create a directory:
There are two methods in the file class that can be used to create a folder:
- The mkdir () method creates a folder, returns True if successful, and returns False if it fails. Failure indicates that the path specified by the file object already exists, or that the folder cannot be created because the entire path does not yet exist.
- The mkdirs () method creates a folder and all its parent folders.
The following example creates the "/tmp/user/java/bin" folder:
Importpublicclass createdir { publicstatic void Main (String args[]) { = "/tmp/user/java/bin"; New File (dirname); // Create directory now d.mkdirs (); }}
Compile and execute the above code to create the directory "/tmp/user/java/bin".
Note: Java automatically distinguishes file path delimiters by convention in UNIX and Windows. If you use the delimiter (/) in the Windows version of Java, the path can still be parsed correctly.
Read Directory
A directory is actually a file object that contains other files and folders.
If you create a File object and it is a directory, then calling the Isdirectory () method returns True.
You can extract a list of the files and folders it contains by calling the list () method on the object.
The example shown below shows how to use the list () method to check what is contained in a folder:
Import Java.io.File; public class Dirlist {public static void main (string args[]) { string dirname = "/tmp"; File F1 = new file (dirname); if (F1.isdirectory ()) { System.out.println ("directory" + dirname); String s[] = F1.list (); for (int i=0; i < s.length; i++) { file F = new File (dirname + "/" + s[i]); if (F.isdirectory ()) { System.out.println (S[i] + "is a directory"), or else { System.out.println (S[i] + "is a file"); c12/>}}} else { System.out.println (dirname + "not a Directory"); }}}
The results of the above example compilation run as follows:
Directory/tmpbin is a directory lib is a directory demo is a directory test.txt is a file readme is a file index.html is a file include is a directory
Resources:
Http://www.tuicool.com/articles/NFBvye
JAVA Stream (stream), file, and IO