Io File class and serialized object based on Java

Source: Internet
Author: User

File class

Used to encapsulate files or folders into objects.

Convenient operations on file and folder attributes

The stream can only operate on data, but cannot operate on directories.

Static attribute file. Separator name Separator

File Creation steps

1. encapsulate it into a file new file ()

2. Create File. createnewfile ();

Common File methods:

File Creation

Boolean createnewfile () creates a file at the specified position. If the file exists, it is not created. False is returned.

The output stream creates a file. If the file exists, it overwrites the original file.

Boolean mkdir () create a level-1 directory

Boolean mkdirs () to create a multi-level directory

When determining whether a file is a file or directory, you must first determine whether the encapsulated content of the file object exists.

Delete

Boolean Delete (): false if deletion fails.

Void deleteonexit ();

Judgment

Whether the Boolean exists () file exists

Isfile ()

Isdiricloud ()

When calling the list () method of a file, the file object must encapsulate a directory, which must also exist.

If the list () method is called for a specific file, a null pointer is thrown.

File F = new file ("C: \ ABC ");

String [] names = f. List ();

Print the directory cyclically Using Recursion

Attention should be paid to recursion

1. Restrictions

2. Pay attention to the number of recursion times to avoid memory overflow.

Delete an object

Directory with content, delete from the inside out

Recursively call to delete an object first, and delete a folder

* The file cannot be deleted from the recycle bin in Java.

Properties is a subclass of hashtable and has the characteristics of Map sets. The stored key-value pairs are all strings and are a collection container combined with IO technology.

Feature: it can be used in key-value pairs.

Getpropertynames ()

Getproperty (string key );

Setproperty (string key, string value );

Obtain the attribute

Jdk1.5 propertynames ()

Jdk1.6 stringpropertynames ()

Properties can load the file TXT in the stream

Read File Content from stream

1. byte stream

2. Streams

Filereader FS = new filereader(%1.txt ");

Properties pp = new properties ();

Pp. Load (FS );

Modify files on a hard disk

Setproperty (string key, string value );

Save

store(OutputStream out,String comments)  

store(writer out,String comments)

commentsComment text

Print Class

    PrintStreamClass

Constructor parameter type

1.fileObject File 2. String path string 3. byte output stream

PrintWriterClass

Constructor parameter type

1.    fileObject File 2. String path string 3. byte output stream outputstream 4. character output stream writer 

Sequential stream

Sequenceinputstream class

Merge multiple streams

File cutting and merging

Object stream

Objectinputstream and objectoutputstream

Key serializable interfaces are serialized under JDK Control

Class t implements serializable

{

Int I = 10;

Int J = 9;

Double D = 23;

Int K = 15;

}

Fileoutputstream Fos = new fileoutputstream ("D:/Java/IO/testobject. dat ");

Objectoutputsteam OOS = new objectoutputstream (FOS );

T = new T ();

T. k = 8;

Oos. writeobject (t); // write the object to the file

Fileinputstream FCM = fileinputstream ("D:/Java/IO/testobject. dat ");

Objectinputstream OIS = new objectinputstream (FCM );

T tread = (t) Ois. readobject (); // read the object

System. Out. println (TREAD );

Member variables modified by the keyword transient are not considered during serialization, that is, they are not written to the hard disk.

 

The keyword externalizable interface provides the serial number interface for users.

 

 

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.