tags:

views:

121

answers:

3

I came up with the follwing code...my Document is not created. I dont get any Error messages. Regarding the Java API i did everything in the right order. The directory is created in the right way...so i dont have to worry about that.

Anyone got an idea or an hint where to look at?

  public static void main(String[] args) throws Exception{
    String path = null;
    String destination = "/myfolder/test/" + createRandomPath(path);
    try {
        boolean status;
        status = new File(destination).mkdir();
    } catch (Exception e) {
        System.out.println("Fehler: " + e.getMessage());
    }

    File document = new File(destination + "temp.docx");

    //Edit: Here is the solution..Thank you
    document.createNewFile();

  }
  static String createRandomPath(String path){
     UUID uuid = UUID.randomUUID();
     path = uuid.toString().replace('-', 'A').substring(0,9);
     System.out.println(path);
     return path;
  }
+3  A: 

you need to call createNewFile() to, err, create a new file on disk if that's what you're asking.

so you need code of the form:

File f = new File("C:\\temp\\newfile.txt");
//new file does not exist on disk yet
f.createNewFile();
//f should be created on disk now
James B
you are right..:-) arrrgh got the answer the whole time in front of my nose and couldnt find it.
bastianneu
i added your answer to my code... with a "Solution" Comment. Thank you..Upvote
bastianneu
cheers, Merry Christmas!
James B
+1  A: 

The File Document... line creates an instance of the File class in memory. As James B says, you need to then create the file, perhaps with something like this:

File doc = new File(destination + "temp.docx");
doc.createNewFile();

(Remember to wrap this in a try... catch as you will have to trap IOException)

Ben Poole
thank you...useful advice
bastianneu
A: 

Hi, I have modified and executed at my end, I have modified the path and executed ""c:\myfolder\test" + createRandomPath(path);" It works.

 public static void main(String[] args) throws Exception{
    String path = null;
    String destination = "c:\\myfolder\\test" + createRandomPath(path);
    try {
        boolean status;
        status = new File(destination).mkdir();
    } catch (Exception e) {
        System.out.println("Fehler: " + e.getMessage());
    }

    File document = new File(destination + "temp.doc");

    //Edit: Here is the solution..Thank you
    document.createNewFile();

  }
  static String createRandomPath(String path){
     UUID uuid = UUID.randomUUID();
     path = uuid.toString().replace('-', 'A').substring(0,9);
     System.out.println(path);
     return path;
  }
Crickey
Uh, did you read the comments and the answers?
BalusC