import java.io.File ;
import java.io.IOException ;
public class FileDemo01{
public static void main(String args[]){
File f = new File("d:\\test.txt") ; // 实例化File类的对象
try{
f.createNewFile() ; // 创建文件,根据给定的路径创建
}catch(IOException e){
e.printStackTrace() ; // 输出异常信息
}
}
};
import java.io.File ;
import java.io.IOException ;
public class FileDemo02{
public static void main(String args[]){
System.out.println("pathSeparator:" + File.pathSeparator) ; // 调用静态常量
System.out.println("separator:" + File.separator) ; // 调用静态常量
}
};
import java.io.File ;
import java.io.IOException ;
public class FileDemo03{
public static void main(String args[]){
File f = new File("d:"+File.separator+"test.txt") ; // 实例化File类的对象
try{
f.createNewFile() ; // 创建文件,根据给定的路径创建
}catch(IOException e){
e.printStackTrace() ; // 输出异常信息
}
}
};
import java.io.File ;
import java.io.IOException ;
public class FileDemo04{
public static void main(String args[]){
File f = new File("d:"+File.separator+"test.txt") ; // 实例化File类的对象
f.delete() ; // 删除文件
}
};
import java.io.File ;
import java.io.IOException ;
public class FileDemo05{
public static void main(String args[]){
File f = new File("d:"+File.separator+"test.txt") ; // 实例化File类的对象
if(f.exists()){ // 如果文件存在则删除
f.delete() ; // 删除文件
}
}
};
import java.io.File ;
import java.io.IOException ;
public class FileDemo06{
public static void main(String args[]){
File f = new File("d:"+File.separator+"test.txt") ; // 实例化File类的对象
if(f.exists()){ // 如果文件存在则删除
f.delete() ; // 删除文件
}else{
try{
f.createNewFile() ; // 创建文件,根据给定的路径创建
}catch(IOException e){
e.printStackTrace() ; // 输出异常信息
}
}
}
};
import java.io.File ;
import java.io.IOException ;
public class FileDemo07{
public static void main(String args[]){
File f = new File("d:"+File.separator+"mldn") ; // 实例化File类的对象
f.mkdir() ; // 创建文件夹
}
};
import java.io.File ;
import java.io.IOException ;
public class FileDemo08{
public static void main(String args[]){
File f = new File("d:"+File.separator) ; // 实例化File类的对象
String str[] = f.list() ; // 列出给定目录中的内容
for(int i=0;i<str.length;i++){
System.out.println(str[i]) ;
}
}
};
import java.io.File ;
import java.io.IOException ;
public class FileDemo09{
public static void main(String args[]){
File f = new File("d:"+File.separator) ; // 实例化File类的对象
File files[] = f.listFiles() ; // 列出全部内容
for(int i=0;i<files.length;i++){
System.out.println(files[i]) ;
}
}
};
import java.io.File ;
import java.io.IOException ;
public class FileDemo10{
public static void main(String args[]){
File f = new File("d:"+File.separator) ; // 实例化File类的对象
if(f.isDirectory()){ // 判断是否是目录
System.out.println(f.getPath() + "路径是目录。") ;
}else{
System.out.println(f.getPath() + "路径不是目录。") ;
}
}
};
import java.io.File ;
import java.io.IOException ;
public class FileDemo11{
public static void main(String args[]){
File my = new File("d:" + File.separator) ; // 操作路径
print(my) ;
}
public static void print(File file){ // 递归调用
if(file!=null){ // 判断对象是否为空
if(file.isDirectory()){ // 如果是目录
File f[] = file.listFiles() ; // 列出全部的文件
if(f!=null){ // 判断此目录能否列出
for(int i=0;i<f.length;i++){
print(f[i]) ; // 因为给的路径有可能是目录,所以,继续判断
}
}
}else{
System.out.println(file) ; // 输出路径
}
}
}
};