Java IO Learning, encoding of byte stream and character stream

Source: Internet
Author: User

Byte Stream and character stream

Byte stream can handle all types of data (pictures, videos, etc.), the corresponding class in Java is "stream" End 1 bytes = 8 bits binary = The specific storage space character stream can only handle the data of plain text, the corresponding classes in Java are "reader" or "writer" Endings such as kanji, symbols, etc.
Importorg.junit.Test; Public classIotest {/*** Bufferedinputstream bufferedoutputstream * Using byte buffer streams for file replication *@throwsIOException **/@Test Public voidBufferedinputstreamandbufferedoutputstream ()throwsioexception{//new byte input and outputInputStream in =NewFileInputStream ("Hellow.txt"); Bufferedinputstream Bstream=NewBufferedinputstream (in); OutputStream out=NewFileOutputStream ("Hellow2.txt"); Bufferedoutputstream Boutputstream=NewBufferedoutputstream (out); //creating a buffered array  byte[] bytes =New byte[100]; //for Replication  intLen = 0;  while(len = bstream.read (bytes))! =-1) {boutputstream.write (bytes,0, Len); }  //Close the streamBstream.close (); Boutputstream.close (); }/*** Bufferdereader and BufferedWriter * Using buffered streams for file replication *@throwsIOException * **/@Test Public voidBufferdereaderandbufferedwriter ()throwsIOException {//input and output of new charactersReader in =NewFileReader ("Hellow.txt"); BufferedReader Breader=NewBufferedReader (in); Writer out=NewFileWriter ("Hellow2.txt"); BufferedWriter Bwriter=NewBufferedWriter (out); //for ReplicationString line =NULL; inti = 0;  while(line = Breader.readline ())! =NULL) {   if(I! = 0) Bwriter.write ("\ n"); Bwriter.write (line,0, Line.length ()); I++; }  //Close the streamBreader.close (); Bwriter.close (); }/***reader Writer * Use the character input and output stream to complete the copy of the Hello.txt file. * Copy the file as a hello2.txt*/@Test Public voidReaderandwriter ()throwsIOException {//input and output of new charactersReader reader =NewFileReader ("Hellow.txt"); Writer writer=NewFileWriter ("Hellow2.txt"); //defines an array for reading and writing files  Char[] Cbuf =New Char[100]; //read and write files  intLen;  while(len = Reader.read (cbuf))! =-1) {writer.write (Cbuf,0, Len); }  //Close the streamReader.close (); Writer.close (); }/***inputstream OutputStream * Use the byte input and output stream to complete the copy of the Hello.txt file. * Copy the file as a hello2.txt *@throwsIOException*/@Test Public voidTestcopyfile ()throwsioexception{//1. Create an input stream for files that are anchored to Hello.txtInputStream in =NewFileInputStream ("enum class. avi"); //2. Create a file output stream that is anchored to the Hello2.txtOutputStream out =NewFileOutputStream ("Enum Class 2.avi"); //3. Create a byte array for reading and writing filesbyte[] buffer =New byte[1024 * 10]; intLen = 0; //4. Read and write files://in.read (buffer); out.write (buffer, 0, Len); while(len = in.read (buffer))! =-1) {out.write (buffer);}//5. Close the stream resource.out.close (); In.close ();} /*** Test byte output stream outputstream*@throwsIOException*/@Test Public voidTestoutputstream ()throwsioexception{OutputStream out=NewFileOutputStream ("Abcd.txt"); String content= "Www.atguigu.com\nhello java!"; Out.write (Content.getbytes ()); Out.close ();} /*** Test character input stream. reader*@throwsIOException*/@Test Public voidTestreader ()throwsioexception{//use the character input stream to read the contents of the Hello.txt document and output it to the console.Reader reader =NewFileReader ("Hello.txt"); Char[] buffer =New Char[10]; intLen = 0;  while(len = reader.read (buffer))! =-1){  for(inti = 0; i < Len; i++) {System.out.print (buffer[i]);}} Reader.close (); } /*** Test byte input stream inputstream*@throwsIOException*/@Test Public voidTestinputstream ()throwsioexception{//1. A byte input stream was created .InputStream in =NewFileInputStream ("Hello.txt"); //2. Read the contents of the file//2.1 First reads one byte. Inefficient, not recommended.-1 means reading to the end of a file//int result = In.read ();//    //While (Result! =-1) {//System.out.print ((char) result);//result = In.read ();//     } //2.2 reads one group at a time: a set of characters.//returns the number of bytes actually read, or 1 to read to the end of the file//byte [] buffer = new BYTE[10];//int len = 0;//    //While (len = in.read (buffer))! =-1) {//for (int i = 0; i < len; i++) {//System.out.print ((char) buffer[i]);//     } //     } //2.3 reads the contents into a partially contiguous element of the byte array.byte[] result =New byte[1024 * 10]; In.read (Result,10, In.available ()); //3. Close Streaming Resourcesin.close ();} /*** File: A document or directory representing the meaning of physics *@throwsIOException*/@Test Public voidTestfile ()throwsioexception{//1. Create a File objectFile File =NewFile ("Hello.txt"); //2. Test the method of the File object.//2.1 File name-related methodsString FileName =File.getname (); System.out.println (FileName); //2.2 Absolute path to file accessString Path =File.getabsolutepath (); SYSTEM.OUT.PRINTLN (path); //2.3 Renaming a file//File.renameto (New file ("D:\\hello.txt"));//3. Methods related to file detectionSystem.out.println (File.exists ()); File dir=NewFile ("Atguigu"); System.out.println (Dir.isfile ()); //4. Get general information about a fileSystem.out.println (File.length ());//5. File operation related.File file2 =NewFile ("Abcd.txt"); File2.createnewfile (); } } 

Java IO Learning, encoding of byte stream and character stream

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.