1. Classification of IO streams
Character stream
Reader
InputStreamReader (node stream)
BufferedReader (processing streams)
Writer
OutputStreamWriter (node stream)
BufferedWriter (processing streams)
PrintWriter
Byte Stream
InputStream
FileInputStream (Node Stream)
BufferedInputStream (processing streams)
ObjectInputStream (processing streams)
PrintStream
OutputStream
FileOutputStream (node stream)
BufferedOutputStream (processing streams)
ObjectOutputStream (processing streams)
Breakpoint processing stream
RandomAccessfile
2. Usage of IO streams
1. Usage of conversion streams
FileInputStream in = new FileInputStream(newFile("")); Readerreader = new InputStreamReader(in);//Byte to character FileOutputStreamout = new FileOutputStream(newFile("")); Writer writer = new OutputStreamWriter(out);//Charact to byte2. Object serialization, the object needs to implement the Serializable interface
FileOutputStreamfileOutputStream = new FileOutputStream("C://Users//lx//Desktop//Record.txt"); ObjectOutputStreamobjectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(object);//Write object to the specified file objectOutputStream.close(); FileInputStreamfileInputStream = new FileInputStream("C://Users//lx//Desktop//Record.txt"); ObjectInputStreamobjectInputStream = new ObjectInputStream(fileInputStream); object = objectInputStream.readObject();//Read the object object fileInputStream.lose();3. Application of breakpoints
public class Copy extends Thread{//You can use multithread to realize copy longstart;longend;Filesorce;Filetargetdir;publicCopy() {}publicCopy(longstart,long end, File component, File targetdir) {//Use the constructor to pass the length of the copy, the start position of the copy, as well as the target file and source file super(); this.start= start; this.end= end; this.sorce= sort; this.targetdir= targetdir;}@Override publicvoid run(){try{RandomAccessFilesouceRaf = new RandomAccessFile(sorce,"r");RandomAccessFiletargetRaf = new RandomAccessFile(newFile(targetdir,sorce.getName()),"rw");souceRaf.seek(start);targetRaf.seek(start);intlen= 0;byte[]bs = new byte[1024];longseek;System.out.println(start+"---->"+end+this.getName());while((len= souceRaf.read(bs))!=-1){targetRaf.write(bs, 0, len);seek= souceRaf.getFilePointer();//Get breakpoint position if(seek== end){break;}}targetRaf.close();souceRaf.close();}catch (IOException e) {e.printStackTrace();}}}4. Usage of byte streams
public class Test_InputStream {//Use byte streams to obtain text file content, but problems are prone to occur/* //Int length may be out of bounds public static void main(String[] args) throws IOException { InputStream inputStream = new FileInputStream(new File("C://Users//lx//Desktop//test//33.txt")); byte[] b = new byte[inputStream.available()]; inputStream.read(b); String str = new String(b); System.out.println(str); }*/// garbled code may appear public static void main(String[] args) throws IOException {File file = new File("C://Users//lx//Desktop//test//33.txt");InputStream inputStream = new FileInputStream(file);// Statistics the actual length of each read int len = 0;// Declares 1024 bytes per read byte[] b = new byte[2];StringBuffer sBuffer = new StringBuffer(); while((len=inputStream.read(b))!=-1){sBuffer.append(new String(b,0,len));}System.out.println(sBuffer.toString());}}//Copy the file using byte stream public void copy(File sourceFile, File targetDir) {//FileInputStreamfileInputStream = null;FileOutputStreamfileOutputStream = null;fileInputStream= new FileInputStream(sourceFile);FiletargetFile = new File(targetDir,sourceFile.getName());fileOutputStream= new FileOutputStream(targetFile);byte[]b = new byte[1024];intlen = 0;while((len= fileInputStream.read(b)) != -1) {fileOutputStream.write(b, 0, len);}}5. Usage of cached character streams
publicstatic void main(String[] args) throws IOException {//Cached character stream implementation to write files InputStreamin = System.in;Readerreader = new InputStreamReader(in);BufferedReaderbr = new BufferedReader(reader);BufferedWriterbw = new BufferedWriter(new FileWriter(new File("src/1.txt")));Strings="";while((s=br.readLine())!=null) {bw.write(s);bw.newLine();bw.flush();//Character streams Don't forget flush!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!}}Summarize
The above is all the content of this article about the analysis of Java IO stream related knowledge code, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!