Difference between Java character streams and byte streams for file operations _java

Source: Internet
Author: User
Tags string to file

Remember the first time I began to learn Java, the Java IO stream this piece is particularly not clear, so wrote this essay hope to be able to just start learning Java people help, but also convenient for their own inquiries. The Java IO stream is divided into character streams (reader,writer) and byte streams (inputstream,outputstream), and the byte stream is the word stream that reads the contents of the file into an array of bytes and then outputs it to another file. The smallest unit of character stream manipulation is the characters. You can look at the overview of IO streams first:

The following is the first reading and writing of a file through a character stream:

Package lib;
Import Java.io.BufferedReader;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;

Import Java.io.PrintWriter;
  The public class Test {//definition file path filename f = new file ("F:\\test.txt");
    The character stream writes the method public string Writeinfile () throws ioexception{string str = "";
    String count = "";
      try {//Use character stream to read a file BufferedReader bf = new BufferedReader (new FileReader (f));
        while (true) {//reads each row of data and assigns it to STR if ((count = Bf.readline ())!= null) {str = count;
        } else {break;
    }//Close stream bf.close ();
    catch (FileNotFoundException e) {e.printstacktrace ();
  return str;  }//Character stream read method public void Getreader () {try {///where True indicates that the end of the original file content is added, if not written, then add content after emptying the file PrintWriter pw=new
      PrintWriter (New FileWriter (f,true));
      Pw.write ("Test input string to file 2");
    Pw.close (); } CATCH (IOException e) {e.printstacktrace ();
    } public static void Main (string[] args) throws IOException {test test=new test ();
    Enter the string into the file Test.getreader ();
    Reads the corresponding string str=test.writeinfile ();
  System.out.println the contents of the file in the console output ("The file content is:" +str);

 }
}

The key areas of the above code have comments, no longer one by one to repeat, mainly after the use of the flow do not forget to close the good

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

 package com.file.test2;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;

Import java.io.IOException;
    public class TestFile2 {//Use byte streams to read and write files, copy one file to another file public static void main (string[] args) throws IOException {
    The source file F=new file ("D:\\test.txt") to be copied;
    The destination file, f2=new file ("D:\\test2.txt");
    Defines a byte-type array to store the read content byte [] b=new byte[1024];
    int length;
      try {//defines the read stream FileInputStream in=new fileinputstream (f);
      Defines the flow of output to a file FileOutputStream out=new fileoutputstream (F2);
      Outputs the contents of the file to another file while ((Length=in.read (b))!=-1) {out.write (b, 0, length);
      } out.close ();
    In.close ();
    catch (FileNotFoundException e) {e.printstacktrace (); }
  }
}

In the operation of a byte stream, the source file for line 13th must exist. Can change the file path according to need, only need to exist, otherwise will report the file can not find the error, and if you want to read in the console output of the stream of bytes of the contents of the line will be between the 27th and 28 lines Add two code: In.read (b, 0, B.length); System.out.println (New String (b));

The above is the character stream and word throttling related operations, in fact, the code is not difficult, mainly their own understanding, the same problem everyone will have different ways of understanding, of course, for our programmers, in addition to thinking more than to do more. Finally hope that the above content can help you, please continue to support the site.

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.