Java File I/O

Source: Internet
Author: User

File class:

Common methods:

    Boolean exists (): Determines whether a file or directory exists
    boolean isfile (): Determines whether the file is
    boolean isdirectory (): Determines whether the directory
    String GetPath (): returns the relative path name of the file represented by this object
    String GetAbsolutePath (): Returns the absolute path name of the file represented by this object
    String getName (): Returns the name of the file or directory represented by this object
    Boolean Delete (): Deletes the file or directory specified by this object
    boolean createnewfile (): Create a name for an empty file without creating a folder
    long Length (): Returns the length of the file, in bytes, or 0L if the file does not exist

I/O flow

Classification of streams:

According to the direction of flow:

Output streams:outputstream and Writer as base classes

Input stream:inputstream and Reader as base classes

According to the Processing Data Unit Division:

BYTE stream:

BYTE output stream:outputstream base class

BYTE input stream:InputStream base class

Character Stream:

Character output stream:Writer base class

Character input stream:Reader base class

Cases:

1, create the input stream object, responsible for reading d:\ my youth who decides. txt file
FileInputStream fis = new FileInputStream ("d:\\ my youth who decides. txt");
2. Create an output stream object, responsible for writing the contents of the file to C:\myFile\my Prime.txt
FileOutputStream fos = new FileOutputStream ("C:\\myfile\\myprime.txt", true);
3. Create a broker array to hold the contents of each read
byte words [] = new char[1024];
4, through the loop to achieve file read and write
while ((Fis.read ())!=-1) {
Fis.read (words); Read file
Fos.write (words, 0, words.length); Write file
}

Java File I/O

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.