Byte stream and character stream of IO
What's the difference between byte stream and character stream?
Byte stream transmission of any data, picture, video, text, etc
Character stream can only process plain text (character stream is also recommended for plain text)
Computer is the same Oh, so do not use the character stream to deal with non character files.
Because this article is just the beginning of IO, let's start with a brief introduction
1. thought
Four points to grasp the theme
//1 select source
//2 selection flow
//3 operation
//4 release resources
2. code
Byte stream
package IO; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; //1 source of choice //2 choice flow //3 operation // 4 release resources public class Standard file byte stream { public static void main(String[] args) { //Standard file byte stream f= new Standard file byte stream(); f.out(); } public void read() {//Byte file read stream //Selection source File file = new File("king.txt"); //Selection flow InputStream in= null; try { in = new FileInputStream(file); //Operation (segment read) byte [] flush =new byte [128];//Buffer container int len = -1;//Receiving container while((len = in.read(flush))!=-1) { String str=new String(flush,0,len);//Byte array -- > string decoding System.out.println(str); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally {//Release resources if(null!=in) { try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public void out() { //Selection source File file =new File("king.txt"); //Selection flow OutputStream output =null; try { output = new FileOutputStream(file ,true); //operation String s ="Here I am again."; byte [] n=s.getBytes();//Code output.write(n, 0, n.length); output.flush();//Refresh } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally {//Closed flow if(null!=output) { try { output.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
Copy of byte stream file
package IO; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Copies of documents { public static void main(String[] args) { //Copy of file f = new Copies of documents(); f.copy("1.jpg", "Documents tested.jpg"); } public void copy(String srcpath,String destpath) { //Selection source //File file1 =new File("1.jpg"); //File file2 =new File("copy.jpg"); File file1 =new File(srcpath); File file2 =new File(destpath); //Selection flow InputStream in =null; OutputStream out = null; try { in=new FileInputStream(file1); out =new FileOutputStream(file2); //operation byte [] flush = new byte [1024]; int len =-1; while((len=in.read(flush))!=-1) { out.write(flush, 0, flush.length); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //Release resources try { if(null!=out) { out.close(); } } catch (Exception e) { // TODO: handle exception } try { if(null!=in) { in.close(); } } catch (Exception e) { // TODO: handle exception } } }
Character stream
package IO; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; public class File character input / output stream { public static void main(String[] args) { //File character I / O stream f= new File character input / output stream(); f.out(); } public void in() { //Selection source File file = new File("king.txt"); //Selection flow Reader read=null; try { read=new FileReader(file); char [] flush = new char [128]; int len =-1; while((len=read.read(flush))!=-1) { System.out.println(flush); } //operation } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally {//Release resources if(null!=read) { try { read.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public void out() { File file =new File("File character stream test.txt") ; Writer write =null; try { write=new FileWriter(file); String test ="I really don't want to play"; char [] flush =test.toCharArray();//String - > array write.write(flush, 0,flush.length); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(null!=write) { try { write.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }