Java.io package includes almost all operations based input and output desired. All of these streams represent the classes the input source and output destination.
Streaming support a variety of formats Java.io package, such as: basic type, object, character sets, and so localized.
It can be understood as a sequence of a stream of data. The input data stream from a source for read, write data to the output stream represents a goal.
Java provides a powerful and flexible support for I / O, making it more widely used in file transfers and network programming.
Console input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
After BufferedReader object is created, we can use the read () method reads a character from the console, or read a string readLine () method.
A character read from the object to be used BufferedReader read () method, which has the following syntax:
int read() throws IOException
Each call read () method which reads a character from the input stream and to return the character as an integer value. When the end of the stream or -1. This method throws an IOException.
Reading a string from standard input required readLine BufferedReader () method.
String readLine() throws IOException
Version of JDK5 we can also use Java Scanner class to get input console.
Console output
Prior has been introduced, the console output is done by 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 (). In this way, write () can write with and from the console.
The simplest format defined PrintStream write () is as follows:
void write(int byteval)
Reading and Writing Files
A stream is defined as a data sequence. For reading data from the input source stream, the output stream for writing data to a target.
IO stream into a byte stream and character stream, a stream of characters is divided into Reader and Writer, byte stream into InputStream and OutputStream.
FileInputStream
The stream used to read data from a file, its object can be created with the new keyword.
There are several construction methods can be used to create objects.
// constructor. 1 the InputStream F = new new the FileInputStream ( "C: / Java / Hello" ); // constructor 2 File F = new new File ( "C: / Java / Hello" ); the InputStream OUT = new new the FileInputStream (F) ;
FileOutputStream
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.
There are two ways to create a constructor FileOutputStream object.
// constructor. 1 the OutputStream F = new new a FileOutputStream ( "C: / Java / Hello" ) // constructor 2 File F = new new File ( "C: / Java / Hello" ); the OutputStream F = new new a FileOutputStream (F);
Examples
Import the java.io. * ; public class fileStreamTest2 { public static void main (String [] args) throws IOException { File F = new new File ( "a.txt" ); FileOutputStream FOP = new new FileOutputStream (F); // Construction 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 operating system default encoding, the windows are GBK Writer.append ( " Chinese input " ); // written to the buffer Writer.append ( "\ r \ the n-" ); // wrap Writer.append ( "English" ); // cache is flushed red, written to a file, if the content has not written the following , and also written directly to close writer.Close (); // Close written to the stream, while the contents of the buffer will write the file, so the above commented fop.close (); // close the output stream, delivery systems resource FileInputStream FIP = new new FileInputStream (F); // build the object FileInputStream InputStreamReader Reader = new new InputStreamReader (FIP, "UTF-. 8" ); // build InputStreamReader objects, and writing the same encoding SB StringBuffer = new new StringBuffer (); the while (reader.ready ()) { sb.append (( char ) reader.Read ()); // switch to char added StringBuffer object } System.out.println (SB. toString ()); reader.Close (); // Close for read fip.close (); // close the input stream, free system resources } }
Java file directory
Create a directory
File class has two methods can be used to create a folder:
- 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.
import java.io.File; public class CreateDir { public static void main(String args[]) { String dirname = "/tmp/user/java/bin"; File d = new File(dirname); // 现在创建目录 d.mkdirs(); } }
Note: the Java automatically according to the agreed resolution file path separator in UNIX and Windows. If you use a separator in the Windows version of Java, (/), the path is still to be resolved 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 a call isDirectory () method returns true.
By calling on the object list () method to extract It contains a list of files and folders.
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("目录 " + 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" ); } the else { System.out.println (S [I] + "is a file" ); } } } the else { System.out.println (dirname + "is not a directory" ); } } }
To delete a file or directory
Delete files can be used java.io.File.delete () method.
The following code will delete the directory / tmp / the Java / , to note that when you delete a directory, you must ensure that no other files in this directory can delete properly, otherwise it will delete failed.
Import java.io.File; public class DeleteFileDemo { public static void main (String args []) { // Here modify its test directory File Folder = new new File ( "/ tmp / Java /" ); DeleteFolder (Folder) ; } // delete files and directories public static void DeleteFolder (file Folder) { file [] files = folder.listFiles (); IF (files =! null ) { for (file F: files) { IF (f.isDirectory ( )) { DeleteFolder (F); } else { f.delete(); } } } folder.delete(); } }