这段代码也许对你有用
import java.io.*;
public class Creatfile {
String path;
public Creatfile(String path,String filename) {
this.path = path;
File file = new File(path);
File fileparent = file.getParentFile().getParentFile();
if(fileparent.isDirectory()){
try{
String path1 = fileparent.getPath()+"\\"+filename;
File creatfile = new File(path1);
if(!creatfile.exists())
creatfile.createNewFile();//创建文件
//creatfile.mkdir();//创建文件夹
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String filepath = "D:\\test\\mm\\gg";
String filename = "hehe.txt";
Creatfile creatfile1 = new Creatfile(filepath,filename);
}}
import java.io.*;
public class Creatfile {
String path;
public Creatfile(String path,String filename) {
this.path = path;
File file = new File(path);
File fileparent = file.getParentFile().getParentFile();
if(fileparent.isDirectory()){
try{
String path1 = fileparent.getPath()+"\\"+filename;
File creatfile = new File(path1);
if(!creatfile.exists())
creatfile.createNewFile();//创建文件
//creatfile.mkdir();//创建文件夹
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String filepath = "D:\\test\\mm\\gg";
String filename = "hehe.txt";
Creatfile creatfile1 = new Creatfile(filepath,filename);
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货