File in Java

Source: Internet
Author: User

1. What is a stream?
The flow in Java is an abstract concept, and when a program needs to read data from a data source, it opens a stream of data that can be a file, memory, or network, and so on.
2. Manipulating files or directory properties using the file class
public class Kt {
public static void Main (string[] args) {
File f= New file ("Src/zhu.txt");---> Create a File Object
"Src/zhu.txt" indicates the file path name pointed to
System.out.println (f);
Determine if a file exists
Boolean flag = F.exists ();
SYSTEM.OUT.PRINTLN (flag);
Determine if it is a file
Flag = F.isfile ();
SYSTEM.OUT.PRINTLN (flag);
Whether it is a directory
Flag = F.isdirectory ();
SYSTEM.OUT.PRINTLN (flag);
Get File relative path
String path = F.getpath ();
SYSTEM.OUT.PRINTLN (path);
Gets the absolute path of the file
Path =f.getabsolutepath ();
SYSTEM.OUT.PRINTLN (path);
Get the name
String name = F.getname ();
SYSTEM.OUT.PRINTLN (name);
Delete files or directories
Flag = F.delete ();
SYSTEM.OUT.PRINTLN (flag);

Return file length

Long L = f.length ();

    System.out.println (l);

}
}
Operation Result:

E:\Zhu.txt
True
True
False
E:\Zhu.txt
E:\Zhu.txt
Zhu.txt
True

0
Note: 1) First create a text file
Example: Zhu.txt
2) in the Windows operating system, the delimiter in the file path name can use a forward slash "/" such as "Src/zhu.txt"
You can also use the backslash "\", but it must be written as "\ \" where the first one represents an escape character, such as "Src\\zhu.txt"
3) file paths are divided into two types:
Relative paths: Non-web projects are relative to the project as a starting point,< > (example: Src/a.txt)
Absolute path: D:/bin/a.txt (starts with a drive letter)

4) file cannot manipulate the contents of files

File in Java

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.