byte stream and character stream and simple example

Source: Internet
Author: User

java io is a very basic knowledge point in Java, and it should be used frequently for communication and project Java IO that do not involve databases. Java io is divided into byte stream and character stream, distinguishing between byte stream and character stream is the starting point of mastering Java IO.

The byte stream, the smallest unit is bytes, and the X-modem and Y-modem protocol transmission units in the communication protocol are word streams. The main classes related to byte stream in Java io are: Inputstream,outputstream,fileinputstream,fileoutputstream. The main classes related to character streams are: Writer,reader,filewriter,filereader. The class that links a character stream to a byte stream is: Inputstreamreader,outputstreamwriter. Byte stream in the process of reading and writing files does not involve buffering, character stream in the process of reading and writing files involves buffering, for the underlying information transmission using the word stream is better, there is no corresponding character; and for the application layer of information read and write using a character stream is good, the English alphabet accounted for one byte, Chinese accounted for two bytes, Using a character stream does not appear to truncate Chinese. The overall framework of the IO stream is:

The graph source is:, clearly shows the byte stream and character stream related classes and inheritance relationship.

You can also learn more about byte stream and character stream through related topics.

1. There are two files 1.txt,2.txt, request to read the file and the information in the file two, the read information is cross-written to 3.txt, if only the interview can be considered simple, do not consider the problem of insufficient memory. If the problems in the actual project need to be considered comprehensively. The following code is only for the general written test:

Import;import;import;import;public class Testfilereader {public string FileReader (string addr) {File File = new file (addr); char[] ch = new char[2000]; String sb= "", int flag;try {FileReader fr = new FileReader (file), while (flag = (ch))!=-1) {SB + = String.valueof (CH);} Fr.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} return SB;} public void FileWriter (String addr,string str) {File File = new file (addr); try {filewriter fw = new FileWriter (File); Fw.wri Te (str); Fw.flush (); Fw.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} public static void Main (string[] args) {Testfilereader TFR = new Testfilereader (); String str1; String str2; String str= ""; str1 = Tfr.filereader ("D:/1.txt"); str2 = Tfr.filereader ("D:/2.txt"); int i;for (I=0;i<str1.length (); I + +) {str = str + str1.charat (i); if (Str2.length () >i) {str = str + str2.charat (i);}} if (i<str2. Length ()) {str = str + str2.substring (i, str2.length ());} Tfr.filewriter ("D:/3.txt", str);}}
The code above implements reading two files and then cross-writing to another file. is to use string to accept the contents of the file without considering the memory issue.

2. Copy all files under one directory to another directory.

Import;import;import;import; Import;import;import;import;import;public class Testfilereader {public void fileCopy (String sourcepath, String newpath) {FileInputStream FIS = null; FileOutputStream fos = null;datainputstream dis = Null;dataoutputstream dos = null;try {fis = new FileInputStream (sourcePa TH);d is = new DataInputStream (FIS); fos = new FileOutputStream (newpath);d os = new DataOutputStream (FOS); int Temp;try {Whil E (( ())!=-1) {dos.write (temp);} Fis.close ();d is.close (); Fos.close ();d os.close (); catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} public void Foldercopy (String sourcepath,string newpath) {File File = new file (sourcepath); if (!file.exists ()) { File.mkdir ();}file[] files = file.listfiles (); for (int i=0;i<files.length;i++) {this.filecopy (sourcepath+ "/" +files[i].getname () , newpath+ "/" +files[i].getname ());}} public static void Main (string[] args) {Testfilereader TFR = new Testfilereader (); Tfr.foldercopy ("D:/test", "D:/test1");}}
The above code is still not tight enough, but as the answer to the question is enough, not strict enough to deal with the SourcePath folder in the case. byte stream and character stream and simple example

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.