java中讲讲File的用法

File的用法
马克-to-win:sun公司设计File类,本身不能用来读数据或写数据。(要想读写数据,必须和其它io流的类配合使用,比如 FileInputStream等)File类的功能就是对磁盘上的文件或目录做一些非读写方面的工作,比如看看文件在哪个目录,哪天创建的,创建个新空文件等。马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。



例:2.1.1

import java.io.*;
public class TestMark_to_win {

    public static void main(String args[]) throws Exception {
/*File(String parent, String child) Creates a new File instance from a
parent pathname string and a child pathname string.the parent  pathname is taken to denote a directory, and the child pathname string is taken to denote either a directory or a file.  */
        File f1 = new File("c:\\tmp", "tmp\\1.txt");
 //File f1=new File("1.txt");
/*getName就是lastname*/
        System.out.println("getName is " + f1.getName());
/* getPath Returns: The string form of this abstract pathname。即原样返回。 */
        System.out.println("getPath is" + f1.getPath());
/* getAbsolutePath: Returns the absolute pathname string of this
abstract pathname. If this abstract pathname is already absolute,
then the pathname string is simply returned as if by the getPath()
method.if you use File f1=new File("1.txt"); you can see the
difference.结果会变成
getName is 1.txt
getPath is1.txt
Absolute Path is D:\eclipseJee\t1\1.txt
not */
        System.out.println("Absolute Path is " + f1.getAbsolutePath());
        System.out.println(f1.exists() ? "exist" : "not");
    }
}
结果是:
getName is 1.txt
getPath isc:\tmp\tmp\1.txt
Absolute Path is c:\tmp\tmp\1.txt
not




例:2.1.2

import java.io.*;
public class TestMark_to_win {
    public static void main(String[] args) {
        File path = new File("c:/");
        /*Returns an array of strings naming the files and directories .*/
        String[] list = path.list();
        for (int i = 0; i < list.length; i++)
            System.out.println(list[i]);
    }
}
结果是:

$360Section
1.txt
2.txt
2_copy.txt
3.txt
360Rec
360SANDBOX
360安全浏览器下载
4.txt
91UserData
Adobe
AUTOEXEC.BAT
AVScanner.ini
BlueStacks_Green
boot.ini
bootfont.bin
Config.Msi
CONFIG.SYS
data.txt
dell
desktop.ini
Documents and Settings
Drivers
EasyGameUserData
FFOutput
InstallConfig.ini
IO.SYS
KPCMS
KwDownload
MSDOS.SYS
MSOCache
my_vod_caching
NTDETECT.COM
ntldr
out.txt
pagefile.sys
ppsfile
Program Files
QMBackup
QMDownload
RECYCLER
screenshot.png
SecurityScanner.dll
SogouPY
StormMedia
System Volume Information
temp
uacdump
vcredist_x86.log
WINDOWS




例:2.1.3(初学者可略过)

import java.io.*;
class MyFilenameFilter implements FilenameFilter {
    /*public boolean accept(File dir,String name) Tests if a specified file
should be included in a file list.\u00A0
Parameters: dir - the directory in which the file was found. qixy: dir is
the caller in the dir.list(myFilenameFilter) name - the name of the
file.\u00A0 Returns: true if and only if the name should be included in
the file list; false otherwise. */
    public boolean accept(File dir1, String name) {
        System.out.println("here" + dir1.getAbsoluteFile());
        System.out.println(name + "hereend");
        boolean flag = (name.indexOf(".txt") != -1);
        return flag;
    }
}

public class TestMark_to_win {
    public static void main(String[] args) throws IOException {
        File dir = new File("c:");
        MyFilenameFilter myFilenameFilter = new MyFilenameFilter();
        /*String[] list (FilenameFilter filter) Returns an array of strings
naming the files and directories in the directory denoted by this
abstract pathname that satisfy the specified filter. 马克-to-win: if dir has
10 files. list method will call accept(File dir, String name1) 10
times, name1 is the\u00A0 name of one of the 10 files which is inside
this directory.\u00A0*/
        String[] names = dir.list(myFilenameFilter);
        for (int i = 0; i < names.length; i++) {
            System.out.println(names[i]);
        }
    }

}

 

result is:

herec:\
$360Sectionhereend
herec:\
1.txthereend
herec:\
InstallConfig.inihereend
herec:\
my_vod_cachinghereend
herec:\
StormMediahereend
herec:\
System Volume Informationhereend
herec:\
temphereend
herec:\
test.txthereend
herec:\
uacdumphereend
herec:\
vcredist_x86.loghereend
herec:\
WINDOWShereend
1.txt
1_c.txt
2.txt
2_copy.txt
3.txt
4.txt
data.txt
out.txt
test.txt