Java file object operation

zhaozj2021-02-11  205

When we perform file operation, you need to know some information about the file. The FILE class provides some member functions to manipulate the files and get some files.

1. Create a new file object

You can create a new file object with the following three methods:

File myfile; myfile = new file ("etc / motd");

or

Myfile = new file ("/ etc", "motd"); // More useful if The Directory or FileName Are Variables

or

File mydir = new file ("/ etc"); myfile = new file (MyDir, "MOTD");

These three methods depends on the way you visit the file. For example, if you use only one file in your application, the structure of the first creation file is the easiest. But if you open several files in the same directory, the second or third structure is better.

2, file testing and use

Once you have created a file object, you can use the following member functions to get the file related information:

File Name: String getName () path: string getpath () string getabslutepath () string getparent () Boolean Renameto (File newname)

Document test:

Boolean EXISTS (),

Boolean canwrite (),

Boolean canread (),

Boolean isfile (),

Boolean isdirectory (),

Boolean isabsolute ().

General File Information Long lastmodified () long length ()

Directory Usage Boolean Mkdir () String [] List ()

3, file information acquisition example

Here is a program for basic information of a separate display file, files through command line parameters:

转载请注明原文地址:https://www.9cbs.com/read-4343.html

New Post(0)