File Class
Used to encapsulate a file or directory as an object
Facilitates processing of file or directory information
The file object can be passed as a parameter to the stream for manipulation
Common methods of File classCreate
boolean
createNewFile():创建新文件,如果文件存在,返回false
boolean
mkdir():创建目录,只能创建一级目录
boolean
mkdirs():创建目录,可创建多级目录
Delete
boolean
delete():删除文件,如果文件不存在,返回false,如果删除的是目录,需要确保目录是空的
void
deleteOnExit():程序结束时删除文件
Judging
boolean
exists():判断文件是否存在
boolean
isFile():判断是否是一个文件
boolean
isDirectory():判断是否是目录
boolean
isHidden():判断文件是否隐藏
boolean
isAbsolute():判断是否是绝对路径
boolean
canExecute():判断文件可否执行
boolean
canRead():判断文件是否可读
boolean
canWrite():判断文件是否可写
Get information
String
getName():获取文件名
String
getParent():获取文件路径
String
getPath():获取封装进对象的路径
File
getAbsoluteFile():获取绝对路径文件对象
String
getAbsolutePath():获取绝对路径
long
lastModified():获取最后修改时间
long
length():获取大小
static File[]
listRoots():静态方法,获取系统可用根目录
String[]
list():获取目录中文件的文件名
String[]
list(FilenameFilter filter):过滤文件名并获取
Simple application of file classprint. java files
2: //print. java files in the specified directory
3: private static void FileFilter ()
4: {
5: File F = new file ("D:\\code\\java_blog_code");
7: //filenamefilter to set filter
8: string[] s = f.list (new FilenameFilter ()
9: {
Ten: Public Boolean Accept (File dir,string name)
One: {
: if (Name.endswith (". Java"))
: return true;
: Else
: return false;
: }
: });
: For (String ss:s)
: {
: System.out.println (ss);
: }
: }
Print a hierarchical catalog file
1://Print tabs by level
2:private static void Printlevel (int level)
3: {
4: For (int i = 0, i < level; I + +)
5: {
6: System.out.print ("\ t");
7: }
8:}
10://Use recursion to print files of the specified directory hierarchically
11:private static void Showfiles (File dir,int level)
12: {
: //Print directory Name
: printlevel (level);
: System.out.println (Dir.getname ());
: file[] files = dir.listfiles ();
18:
: For (File f:files)
: {
: if (F.isdirectory ())
: //If it is a directory, the incoming method is recursive
At: showfiles (f,level+1);
: Else
: {
: //Print file list
: printlevel (level+1);
: System.out.println (F.getname ());
: }
: }
31:}
Properties Class
The file configuration information specialized class, Hashtable subclass, Internal holds some file attribute key value pair information, if the more complex information can use the XML storage, the simple key value pair uses the properties access
Properties Common Methods
String
getProperty(String key):通过key获取value信息
void
load(InputStream inStream):从字节流加载键值对信息
void
load(Reader reader):从字符流加载键值对信息
Object
setProperty(String key, String value):设置键值对信息
void
store(OutputStream out, String comments):将键值对信息导入到字节流
void
store(Writer writer, String comments):对键值对信息导入到字符流
Properties Simple Applicationsimulation Limit program run times
1:import java.io.*;
2:import java.util.*;
4:/*
5: Simulation Limit program run times
6: */
8: {
9: Public static void Main (string[] args) throws IOException
Ten: {
11:
: Properties Prop = new properties ();
: //import Info.txt byte stream information
: prop.load (New FileInputStream ("Info.txt"));
16:
: //Get program run times
: String times = Prop.getproperty ("Times");
19:
: //If first run, set number of times is 1
: if (times = = null)
: Prop.setproperty ("Times", "1");
: //If the number of times is greater than 5, print the payment information and exit the program
: Else if (Times.compareto ("4") > 0)
: {
: System.out.println ("Times greater than 5!please donate money!");
: system.exit (0);
: }
: //program within the allowable number of runs, get the number of times and add 1
: Else
: {
: Prop.setproperty ("Times", "" "+ (Integer.parseint (times) +1));
: }
34:
: //write a new number of settings to the info file
: Prop.store (New FileOutputStream ("Info.txt"), "Times");
37:
* //program theme, can ignore
Max: for (int i = 0; i <; i + +)
Max: {
In: System.out.println ("i =" +i);
: }
43:
44:
: }
48:}
Category: Java Basic notes
File object in Java IO Stream and properties class