Java io Detailed

Source: Internet
Author: User

Java io Details (i)------File class We know that the path delimiter between the platforms is different. ①, for UNIX platforms, the absolute pathname prefix is always "/". There is no prefix for relative path names.  The abstract path name that represents the root directory has the prefix "/" and the empty name sequence. ②, for Microsoft Windows platforms, the prefix of the path name that contains the drive specifier consists of a drive letter followed by ":", which may be followed by "\ \" If the pathname is absolute. The UNC pathname is prefixed with "\\\\";  The host name and share name are the first two names in the name sequence without a prefix for the relative path name of the specified drive. So in order to block the delimiter differences between the various platforms, we can use the fields provided by Java as we construct the File class (see 2nd below).
12 System.out.println (file.separator);//output \ System.out.println (file.pathseparator);//output;
So we can see: File.pathseparator refers to a delimiter that separates successive paths strings File.separator is a 2 of the directory used to separate the same path string, and the constructor method of the File class how to use the above construction method, see the following example:
12345678910111213141516171819202122 Do not use the delimiter fields provided by Java, note that this is only valid on Windows platforms         file F1 = new  File ("D:\\io\\a.txt");         //using the delimiter provided by Java          file F2 = new file ("D:" +file.separator+ "IO" +file.separator+ "A.txt");         system.out.println (F1);//Output d:\io\a.txt           SYSTEM.OUT.PRINTLN (F2);//Output d:\io\a.txt                  //file (File Parent, String Child)          //creates a new file instance from the parent abstract path name and the subpath name string.         file f3 = new file ("D:");         file f4 = new file (F3, "IO");         SYSTEM.OUT.PRINTLN (f4);  //d:\io                  //file (String pathname)          //creates a new file instance by converting the given pathname string to an abstract path name.         file f5 = new file ("D:" +file.separator+ "IO" + file.separator+ "A.txt");         system.out.println (F5);  //D:\IO\ a.txt                 // File (string parent, String child)         //creates a new file instance from the parent pathname string and the subpath name string.         file f6 = new file ("D:", "Io\\a.txt");         system.out.println (f6);  //d:\io\a.txt
Java io details (ii) classification of------stream one, according to the flow into the input stream and output stream two, according to the transmission of data units divided into byte stream and character stream three, according to the function is divided into node flow and packaging flow Java io detailed (three)------byte input output stream below we use byte output stream The typical implementation of OutputStream FileOutputStream to introduce:
123456789101112131415161718 1. Create the target object, and the output stream indicates which file to save the data to. Do not write the letter, the default file is the root directory of the project         file target = new file ("IO" + file.separator+ "A.txt");         //2, the byte output stream object that created the file, the second argument is a Boolean type, true Represents a file that is written later appended to the data, false to overwrite         outputstream out = new  FileOutputStream (target,true);         //3, specific IO operations (writing data to a file a.txt)             /**              * void Write (int b): Writes a byte to a file               * void Write (byte[] b): Writes all the bytes in array b to the file               * void Write (byte[] b,int off,int len): Len that starts from the off index in array b Bytes written to a file              */ &Nbsp;      out.write;  //writes A to the file          out.write ("AA". GetBytes ());  //writes Aa to file          Out.write ("ABCDEFG". GetBytes (),  1, 5);  //writing bcdef to file          //after the above operation, the data in the A.txt file is aaabcdef                  //4, Close stream resource         out.close ();         system.out.println (Target.getabsolutepath ());  
Java io Detailed (iv)------serialization and deserialization (object flow) 1, what is serialization and deserialization? Serialization: Refers to the Java object data in the heap memory, in some way the object is stored in a disk file or passed to other network nodes (transmission over the network). This process is called serialization. In layman's terms, the process of converting a data structure or object into a binary string is deserialized: The process of restoring object data from a disk file or the object data on a network node to a Java object model.  Which is the process of converting a binary string generated during serialization into a data structure or an object 2, why serialize?  ①, in the distributed system, when the object needs to be transferred on the network, it is necessary to convert the object data into binary form, and the JavaBean object that needs to share the data must be serialized. ②, Server passivation: If the server discovers that some objects are inactive for a long time, the server persists the objects in the local disk file (the Java object is converted to a binary file), and if the server discovers that some objects need to be active, look in memory first Unable to find and then de-serialize our object data in the disk file and revert to the Java object. This will save server memory.  3, how to serialize Java? ①, the class that needs to serialize the object, must implement the serialization interface: Java.lang.Serializable interface (this is a flag interface, there is no abstract method), most classes in Java implement the interface, such as: String,integer②, the bottom will judge,  If the current object is an instance of Serializable, it is allowed to serialize, and the Java object instanceof Serializable to judge.   ③, using object flow in Java to complete serialization and deserialization ObjectOutputStream: serialization by means of the WriteObject () method ObjectInputStream: Deserializing by the ReadObject () method Article reference: http://www.cnblogs.com/ysocean/p/8005694.html

Java io Detailed

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.