java IO stream ------ study notes (3) byte stream buffer & character buffer flow stream & commutations &

java IO stream ------ study notes (3) byte stream buffer & character buffer flow stream & commutations &

Byte stream buffer -BufferedInputStream & BufferedOutputStream

No new method found: file byte stream read (), write () method

The buffer flow can greatly improve the performance of
the buffer flow stream must be the lowest node
only released when the outermost layer of the release (release may be full, the rule: release from inside to outside)

Exercises byte stream files (byte buffer stream)

package Io; 

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 

/** 
* @赌徒 
*/ 
public class copyT4 { 
public static void main(String[] args) throws IOException { 
     copy("data.txt", "copy1.txt"); 

} 

public static void copy(String srcFile,String srcFile1 ) throws IOException { 
     InputStream bis=new BufferedInputStream( new FileInputStream(srcFile)); 
     OutputStream bos=new BufferedOutputStream(new FileOutputStream(srcFile1)); 

     byte b[]=new byte[1024]; 
     int len=-1; 
     while((len=bis.read(b))!=-1) { 
     bos.write(b,0,len); 
} 
     bos.flush(); 
     bos.close(); 
     bis.close(); 
} 
} 

Character buffer flow -BufferedReader & BufferedWriter

The visible part of the method: the file byte stream read (), write () method of
the new method:
the readline () to read a line of characters
NEWLINE () to write a line delimiter

Exercises character stream file (a character buffer stream)

package Io; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.File;  
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 

/** 
* @author 赌徒
*/ 
public class copyT5 { 
public static void main(String[] args) throws IOException { 
     //源头
     File srcFile=new File("data.txt"); 
     File srcFile1=new File("copy2.txt"); 
     //选择流
     BufferedReader br= new BufferedReader(new FileReader(srcFile)); 
     BufferedWriter bw=new BufferedWriter(new FileWriter(srcFile1));
     String lineString=null; 
     //操作
     while((lineString=br.readLine())!=null) { 
         bw.write(lineString); 
         bw.newLine(); 
} 
     bw.flush(); 
     //释放
     bw.close(); 
     br.close(); 
} 

} 

Commutations -InputStreamReader & OutputStreamWriter

 The InputStreamReader (decoding) / OutputStreamWriter (coding): the character stream is a stream of bytes
bridge between, can stream into a character stream of bytes, and can specify the character set as a byte stream, can be processed one character

Operation byte stream (plain text) in the form of a character stream

package Io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 *
 * 1.以字符流的形式操作字节流(纯文本)
 * 2.指定字符集
 * @author 赌徒
 *
 */
public class ConvertT6 {

	public static void main(String[] args) {
		try(BufferedReader isr=new BufferedReader( new InputStreamReader(System.in));
		BufferedWriter osw=new BufferedWriter(new OutputStreamWriter(System.out));) {
			//循环获取键盘的输入(exit退出),输出此内容
			String string="";
			while(!string.equals("exit")) {
				string=isr.readLine();//循环逐行读取
				osw.write(string);//循环写出
				osw.newLine();
				osw.flush();//强制刷新,否则内容会驻留在管道中
			}
			
		} catch (IOException e) {
			System.out.println("操作异常");
		}
	}

}
package Io;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

/**
 * 
 * 1.以字符流的形式操作字节流(纯文本)
 * 2.指定字符集
 * @author 赌徒
 *
 */
public class ConvertText2 {

	public static void main(String[] args) {
		try(BufferedReader isr=
				new BufferedReader(
						new InputStreamReader(
								new URL("http://www.baidu.com").openStream(),"UTF-8"));) {
			       String msg;
			                    
				while ((msg=isr.readLine())!=null) {
					System.out.println(msg);
		}
	}
		catch (IOException e) {
			System.out.println("操作异常");
		}

	}
}

Stream -DataInputStream & DataOutputStream

 A data input stream to allow the application machine independent manner stream read the original Java data types from the input base (to facilitate handling basic data types and the eight strings, not only retains the data retains the data type). Applications that use data written to the output stream data can be read later data input stream.

Structure : have basic internal flow
Note : the order of reading and writing to be consistent

Exercise data stream

package reIo;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * 
 * 1.写出后读取
 * 2.读取顺序与写出保持一致
 * @author 赌徒
 *
 */
public class DataT7 {

	public static void main(String[] args) throws IOException {
 
		//写出
		ByteArrayOutputStream baos=new ByteArrayOutputStream();
		DataOutputStream dos=new  DataOutputStream(new BufferedOutputStream(baos));
		//操作数据类型+数据
		dos.writeUTF("你好");
		dos.writeInt(777);
		dos.writeBoolean(true);
		dos.writeChar('a');
		dos.flush();
		byte []bytes=baos.toByteArray();
		System.out.println(bytes.length);
		//读取
		DataInputStream dis=new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(bytes)));
		//顺序与写出顺序一致
		String string=dis.readUTF();
		int num=dis.read();
		boolean b=dis.readBoolean();
		char c=dis.readChar();
		System.out.println(string);
	}

}
Released four original articles · won praise 12 · views 2370

Guess you like

Origin blog.csdn.net/qq_35577787/article/details/105123530