建立文件路径(50分) 先 File file = new File(你的路径),再用file.exists()判断它是否已经存在,存在的话才能往里面写的,不存在就新建一个。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 下面这样就可以了,你可以看看JAVA的API的,它还有很多种用法。 File file=new File("C:\\bbbb"); file.mkdir(); import java.io.*; class ttt{ public static void main(String[] args) { PrintWriter outf=null; System.out.println(args[0]); try{ outf = new PrintWriter(new BufferedWriter( new FileWriter ("c:\\d"+args[0]+".txt")));//!!!!!!!!!!!!!!!!! outf.println("successful!"); outf.close(); } catch(Exception e){System.out.println(e.toString());} System.out.println("tttttttttttttttttttttttttttttttt"); }}这样就可以文件的名字即是生成 上面的大哥,谢谢你,我要的不是这样,我想("C:\\p\\pra\\"+args[0]+"\\rrrr.txt")))这样,args[0]做为路径,不是文件名,大家帮帮忙,肯定给你加上高分。 下面这段代码就可以了,即使你没有p\pra文件夹,它也会自动给你建好的,你COpy过去运行import java.io.*;public class fileTest { public fileTest(String filepath) { try { File thePath = new File("C:\\p\\pra\\"+filepath); if (thePath.isDirectory() == false) { thePath.mkdirs(); } String fullPath="C:\\p\\pra\\"+filepath+"\\rrrr.txt"; File theFile=new File(fullPath); if (theFile.exists() == true) { theFile.delete(); } theFile.createNewFile(); RandomAccessFile fout = new RandomAccessFile(fullPath, "rw"); fout.writeBytes("I am a Chinese"); fout.close(); }catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { String path=args[0]; fileTest fileTest1 = new fileTest(path); }} import java.io.*;class ttt{ public static void main(String[] args) { PrintWriter outf=null; String sDir = args[0]; File a=null; try { a = new File("C:\\p\\"+sDir); if(!a.exists())a.mkdir(); }catch(Exception e){ } try{ outf = new PrintWriter(new BufferedWriter( new FileWriter (a.getAbsolutePath() +"\\rrrr.txt"))); outf.println("successful!"); outf.close(); } catch(Exception e){System.out.println(e.toString());} System.out.println("tttttttttttttttttttttttttttttttt"); }}//如上添加些添加文件夹的代码就可以了,我测试了,ok 最好是使用File.separator代替"\\"eg: try { File thePath = new File("C:"+File.separator+"p"+File.separator+"pra"+File.separator+filepath); if (!thePath.isDirectory()) { thePath.mkdirs(); } ..... }catch(Exception e) { e.printStackTrace(); } 1-1000中包含1,1000,至少能被2,3,5,7之一整除的数共有多少个 Swing刷新问题,求助 我很迷茫,请高手指点~ 初学java遇到的问题 试问我这样的需求反射可以满足吗? java数组 请教一个I/O读取的问题 java中如何调用带有游标的存储过程???急! 在jbuilder中如何将.java的文件生成.class的文件 狂甩分! 怎么在当前类所在目录,的上级目录建立文件夹? 打包的艺术,心情不错,顺便散分
File file=new File("C:\\bbbb");
file.mkdir();
import java.io.*; class ttt{
public static void main(String[] args) {
PrintWriter outf=null;
System.out.println(args[0]);
try{
outf = new PrintWriter(new BufferedWriter(
new FileWriter
("c:\\d"+args[0]+".txt")));//!!!!!!!!!!!!!!!!!
outf.println("successful!");
outf.close();
}
catch(Exception e){System.out.println(e.toString());}
System.out.println("tttttttttttttttttttttttttttttttt");
}
}
这样就可以文件的名字即是生成
这样,args[0]做为路径,不是文件名,大家帮帮忙,肯定给你加上高分。
public class fileTest {
public fileTest(String filepath) {
try
{
File thePath = new File("C:\\p\\pra\\"+filepath);
if (thePath.isDirectory() == false) {
thePath.mkdirs();
} String fullPath="C:\\p\\pra\\"+filepath+"\\rrrr.txt";
File theFile=new File(fullPath);
if (theFile.exists() == true) {
theFile.delete();
}
theFile.createNewFile();
RandomAccessFile fout = new RandomAccessFile(fullPath, "rw");
fout.writeBytes("I am a Chinese");
fout.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
String path=args[0];
fileTest fileTest1 = new fileTest(path);
}}
public static void main(String[] args) {
PrintWriter outf=null;
String sDir = args[0];
File a=null;
try {
a = new File("C:\\p\\"+sDir);
if(!a.exists())a.mkdir();
}catch(Exception e){
}
try{
outf = new PrintWriter(new BufferedWriter(
new FileWriter
(a.getAbsolutePath() +"\\rrrr.txt")));
outf.println("successful!");
outf.close();
}
catch(Exception e){System.out.println(e.toString());}
System.out.println("tttttttttttttttttttttttttttttttt");
}
}
//如上添加些添加文件夹的代码就可以了,我测试了,ok
eg:
try
{
File thePath = new File("C:"+File.separator+"p"+File.separator+"pra"+File.separator+filepath);
if (!thePath.isDirectory()) {
thePath.mkdirs();
}
.....
}catch(Exception e)
{
e.printStackTrace();
}