Java---2

Source: Internet
Author: User

I'm going to go through Java's IO today, mainly focusing on the file, and of course what zip.

Io as the name implies is the input and output, according to the type of operation into the input stream and output stream, sensory flow is a channel, it gives a variety of data, files ah provide channels. Can be understood by itself. In Java, all IO operations are basically in java.io, and all input streams are subclasses of the abstract class InputStream and reader, the former is the byte input stream, and the latter is the character input stream. There are also OutputStream and writer abstract classes that correspond to this. When a byte stream is read, it returns a byte when it is read, and the character stream reads one or more bytes using the byte throttle (the number of bytes in Chinese corresponds to two, which is 3 bytes in the UTF-8 Code table). First check the specified encoding table and return the found characters. A byte stream can handle all types of data, whereas a character stream will only handle the characters ' data. As long as you are dealing with plain text data, you should prioritize the use of character streams, in addition to byte streams.

The file class is the only object that represents the disk file itself. Operations such as creating (CreateNewFile ()), deleting (delete ()), renaming files, and so on, are implemented by calling methods in the file class. At the same time, this class provides many ways to obtain information about the file itself, such as determining whether a file exists (exits ()), getting the file length (length ()), and the last Modified time (LastModified ()).

When you need to persist the data, you need to use the file input and output stream to establish a connection to the specified file. Both FileInputStream and FileOutputStream are used to manipulate disk files, which says that all two classes are operations on bytes, providing only a way to read bytes and byte arrays, since kanji are two bytes in a file, then if you use these two classes to read Chinese text , there is a great possibility of garbled characters. So for this phenomenon, we should adopt FileReader and FileWriter class. The FileReader stream reads the file sequentially, as long as the stream is not closed, and each call to read reads the rest of the source sequentially until it is close.

Although all open streams are closed automatically at the end of the program, active close is still a good habit after the flow is used, an open stream may take up the system resources, and if the open stream is not closed, the resource may not be available when another program tries to open another stream.

 Public classfiletest{ Public Static voidMain (string[] args) {File file=NewFile ("Word.txt"); Try{FileOutputStream a=Newfileoutputstream (file); bytebuy[]= "Hello Java". GetBytes ();       A.write (Buy);    A.close (); }Catch(excetion e) {e.printstacktrace (); }    Try{FileInputStream B=Newfileinputstream (file); byteby[]=New byte[1024]; intH=B.read (by); System.out.println (NewString (by,0, h));    B.close (); }Catch(excetion e) {e.printstacktrace (); } }}
 file F=new  file ("D:\\hah.txt"  if  (! try   {F.cre            Atenewfile ();  catch   (IOException e) { //  TODO auto-generated catch block              E.printstacktrace (); }}  else  {System.out.println ( "Ha pi" 

The reason I'm writing this here is because I had a very stupid experience, I wrote these two test programs, and then I was thinking about a problem. Why the first program does not use CreateNewFile to create a real file, is it f=new files (...) Will you create a real file? But I clearly remember, file just create a mapping object, not to create a real file, if you have this function, it is createnewfile () What to do ... It turns out that my first program also used FileOutputStream. When using a file output class, if the file does not exist, the file is created automatically, and if the file is entered in the class, an exception IOException is thrown if it does not exist. It's stupid. But I thought for a long time ...

Java---2

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.