先 File file = new File(你的路径),
再用file.exists()判断它是否已经存在,存在的话才能往里面写的,不存在就新建一个。

解决方案 »

  1.   

    下面这样就可以了,你可以看看JAVA的API的,它还有很多种用法。
     
         File file=new File("C:\\bbbb");
          file.mkdir();
      

  2.   


      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");
    }
    }
    这样就可以文件的名字即是生成
      

  3.   

    上面的大哥,谢谢你,我要的不是这样,我想("C:\\p\\pra\\"+args[0]+"\\rrrr.txt")))
    这样,args[0]做为路径,不是文件名,大家帮帮忙,肯定给你加上高分。
      

  4.   

    下面这段代码就可以了,即使你没有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);
      }}
      

  5.   

    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
      

  6.   

    最好是使用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();
        }