IO stream technology is mainly used to solve the operation of data storage to hard disk,
One is called write to hard disk: output
A read from hard disk:: input
IO flows are two ways to manipulate data:
When we read and write data, the program is the first person, so write data to the hard disk, output, read data in input.
Input stream = = > FileInputStream
package com.xingxue.io1; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; /** * Create the file path in disk E as follows: E:/file/src/info.txt; * @author Administrator */ public class TestIo { public static void main(String[] args) { //File input stream String num = "0"; try { // FileInputStream fis = new FileInputStream("E:/info.txt"); File file = new File("E:/info.txt"); FileInputStream fis = new FileInputStream(file); byte b[] = null; try { System.out.println(fis.available()); b = new byte[fis.available()]; } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { fis.read(b); String s = new String(b); System.out.println(s); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Folder and File creation class: File
package com.xingxue.io1; import java.io.File; import java.io.IOException; public class TestFile { public static void main(String[] args) { //create folder File file = new File("E:/file/src"); file.mkdirs(); //create a file File file2 = new File("e:/file/src/info.txt"); try { file2.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Note here that you can't do it in one step. You must create a folder before creating a file
Output stream
java object flow, access object:
ObjectInputStream,FileOutputStream. It stores data in the form of java object, provided that the class of the object must be serialized
public class Map implements Runnable, Serializable{
}
Character stream:
java character stream is Reader and Writer. Through our query API, we found that the string stream can not directly obtain hard disk files. We need to use our byte input stream and byte output stream to obtain files. Therefore, code:
try { //Associating files with byte streams InputStream is = new FileInputStream("e:/a.txt"); //Put byte stream into our basic character stream InputStreamReader isr = new InputStreamReader(is); //Sleeve basic character stream into advanced character stream BufferedReader br = new BufferedReader(isr); System.out.println(br.readLine()); } catch (Exception e) { // TODO: handle exception }
try { OutputStream os = new FileOutputStream("e:/a.txt",true); OutputStreamWriter osw = new OutputStreamWriter(os); BufferedWriter bw = new BufferedWriter(osw); bw.write("Zhang three hello."); bw.close(); osw.close(); os.close(); } catch (Exception e) { // TODO: handle exception }