Java (file I/O)

Source: Internet
Author: User

1 i/0: Input/output

1.1.java.io.file

1.2 Means: File or folder (directory)

1.3 File F = new file ("filename path");

1.4 Note: Relative paths: Non-web projects are relative to the project as a starting point. (Src/a.txt) Recommendations

Absolute path: D:/bin/a.txt (starts with a drive letter)

2. Common methods of documentation

Boolean flag = F.exists ();//File exists

Flag = F.isfile ();//Whether it is a file

Flag = F.isdirectory ();//Whether it is a directory

str = F.getpath ();//Get relative path to file

str = F.getabsolutepath ();//Get the absolute path to the file

str = F.getname (); Get file name

Flag = F.delete ();//delete file

Flag = F.createnamefile ();//Create File

Long = F.length ();//Return file length

Note: fle cannot manipulate file contents;

3.inputstream/outputstream

Files: FileInputStream

3.1 InputStream (input stream)

Data from file to Java code

int read (); Read one byte

int read (byte[]);//read a string of bytes

Long avaliable; File length

3.2 FileInputStream (byte file input stream)

New Fileinputsteam (File);

New FileInputStream ("File path + filename");

3.3 OutputStream (Input stream)

Data from Java code, written to file or other media

void write (bytes); Write a Byte

void Write (byte[]); Write byte array

3.4 FileOutputStream

New FileOutputStream (Flie);

New FileOutputStream ("File path + filename");

New FileOutputStream ("File path + filename", Boolean);

Note:A.boolean: Indicates whether to append to the end of the file, if true, to indicate

Append, False indicates no append (that is, overwrite). Default value is False

B. When creating an FileOutputStream instance

If the corresponding file does not exist, an empty file is created automatically

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.