Java IO byte stream and character stream (iii)

Source: Internet
Author: User
Tags define stream string to file

The IO stream of Java is divided into character stream (Reader,writer) and Byte stream (Inputstream,outputstream), and the byte stream is the name stream that reads the contents of the file into the array of bytes and then outputs it to another file. The smallest unit of the character stream operation is the characters. You can look at an overview of the IO stream first:

The following first reads and writes the file through a character stream:

 1 package lib; 2 3 Import Java.io.BufferedReader; 4 Import Java.io.File; 5 Import java.io.FileNotFoundException; 6 Import Java.io.FileReader; 7 Import Java.io.FileWriter; 8 Import java.io.IOException; 9 Import java.io.printwriter;10 One public class Test {12///Definition file path 14//character ("F:\\test.txt")         Stream writes the method of the public string Writeinfile () throws ioexception{16 string str = "";             try {19//use a character stream to read the file BufferedReader bf = new BufferedReader (new FileReader (f)); 21                     while (true) {22//reads each row of data and assigns it to str23 if (count = Bf.readline ()) = null) {24              str + = count;25} else {break;27}28}29 Close stream Bf.close (); FileNotFoundException catch (e) {E.printstacktrac E ();}34 return str;35}36//Character stream Read method 37     public void Getreader () {# try {39///Where true means to add at the end of the original file content and, if not, to empty the file and add the contents to the PRINTWR         ITER pw=new PrintWriter (new FileWriter (f,true)); Pw.write ("Test input string to file 2"); Pw.close (); 43  } catch (IOException e) {e.printstacktrace ();}46}47 public static void main (string[] args) throws IOException {test test=new test (); 49//Enter a string into the file. Test.getreader (); 51/      /Read the corresponding string str=test.writeinfile (); 53//The contents of the file in the console output System.out.println ("File content is:" +str); 55 }56}

The key points of the above code are commented, it is no longer one by one to repeat, mainly after the use of the stream do not forget to close the good (line 30, line 42)

The file is then manipulated by a byte stream to copy the contents of one file into another file:

 1 package com.file.test2; 2 3 Import Java.io.File; 4 Import Java.io.FileInputStream; 5 Import java.io.FileNotFoundException; 6 Import Java.io.FileOutputStream; 7 Import java.io.IOException; 8 9 public class TestFile2 {10//use a byte stream to read and write to a file, copy one file to another file. public static void Main (string[] args) throws Ioex ception {12//The source file to be copied, f=new files ("D:\\test.txt"), 14//target file ("d:\\ Test2.txt "); 16//Defines an array of type byte that is used to store the read contents of the byte [] b=new byte[1024];18 int length;19 try             {20//definition Read stream FileInputStream in=new fileinputstream (f); 22//define stream 23 for output to file FileOutputStream out=new FileOutputStream (F2); 24//Output the contents of the file to another file. while ((Length=in.read (b))!=-1)         {Out.write (b, 0, length),}28 out.close (); In.close (); 30  } catch (FileNotFoundException e) {e.printstacktrace (); 32       }33}34} 

In the operation of the byte stream, the 13th line of the source file must exist, you can change the file path as necessary, only need to exist, otherwise the file can not find errors, and if you want the console output read the content of the byte stream can be added between the 27th and 28 lines of code: In.read (b, 0, B.length); System.out.println (New String (b));

The above is the character stream and the relevant operation of the byte stream, in fact, the code is not difficult, mainly their own understanding, the same problem everyone will have a different way of understanding, of course, for our programmers, in addition to more thinking and more hands-on. Finally hope that the above content can be helpful to everyone

Java IO byte stream and character stream (iii)

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: info-contact@alibabacloud.com 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.