java中講講File的用法
File的用法
馬克-to-win:sun公司設(shè)計(jì)File類,本身不能用來(lái)讀數(shù)據(jù)或?qū)憯?shù)據(jù)。(要想讀寫數(shù)據(jù),必須和其它io流的類配合使用,比如 FileInputStream等)File類的功能就是對(duì)磁盤上的文件或目錄做一些非讀寫方面的工作,比如看看文件在哪個(gè)目錄,哪天創(chuàng)建的,創(chuàng)建個(gè)新空文件等。馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 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.結(jié)果會(huì)變成
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");
}
}
結(jié)果是:
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]);
}
}
結(jié)果是:
$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(初學(xué)者可略過(guò))
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