javaio
package com.itheima;
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) throws IOException {
File f = new File("d:"+File.separator+"q.txt");
if(f.exists()){
f.delete();
}else{
f.createNewFile();
}
}
}
创建文件夹:
package com.itheima;
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) throws IOException {
File f = new File("d:"+File.separator+"zjs");
f.mkdir();//创建文件夹
}
}
package com.itheima;
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) throws IOException {
File f = new File("d:");
String a[] = f.list();
for(int i=0;i<a.length; i++){
System.out.println(a[i]);
}
}
}
罗列目录中的全部内容:
package com.itheima;
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) throws IOException {
File f = new File("d:");
File[] a = f.listFiles();
for(int i=0;i<a.length; i++){
System.out.println(a[i]);
}
}
}
判断路径是否是目录:
package com.itheima;
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) throws IOException {
File f = new File("d:"+File.separator);
if(f.isDirectory()){
System.out.println(f.getPath());
}else{
System.out.println(f.getPath());
}
}
}