-2

I want to create a file in the project directory so i tried this code and i created a temp directory on the project directory

try {

    File file = new File("temp/"+soCourrier.getHeaders().get("filename").get(0));
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(sCourrier.getBody());
    model.addAttribute("path", file.getAbsoluteFile());

} catch (IOException e) {
    e.printStackTrace();
}

But this gives me an error

java.io.IOException: Le chemin d’accès spécifié est introuvable
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(Unknown Source)
    at controllers.UserController.showCourrierDetail(UserController.java:77)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

can any one help me ?

Wassim Makni
  • 471
  • 2
  • 9
  • 21
  • give absolute path and try – gati sahu Jun 08 '17 at 11:52
  • Java temp files aren't in the project directory – OneCricketeer Jun 08 '17 at 11:53
  • @Jeremy Grand sorry that line should be commented – Wassim Makni Jun 08 '17 at 11:54
  • @gatisahu can you explain me how – Wassim Makni Jun 08 '17 at 11:57
  • @WassimMakni a java application is supposed to be portable, creating files with relative paths is pretty much always bound to fail. Thus, please decide on a folder in which you want your file to be written, and get its path from the filesystem root. And create parent directories before creating the file. – Jeremy Grand Jun 08 '17 at 12:00
  • @JeremyGrand i just said that i created a directory named temp in the project directory – Wassim Makni Jun 08 '17 at 12:04
  • I suggest you have a look here: https://stackoverflow.com/questions/13011892/how-to-locate-the-path-of-the-current-project-in-java-eclipse – Paul Karam Jun 08 '17 at 12:05
  • @WassimMakni Sorry but WHAT is the "project directory" ? is that your source files folder ? Do you realize that your sources files will be compilated and packaged in a single executable object ? Thus, whatever your "project directory" is have to be defined arbitrarily. Once you defined it, create every file relative to this directory, and never expect any `new File("./foobar")` to ever work. – Jeremy Grand Jun 08 '17 at 12:10

1 Answers1

0

It seems like you are using / on a Windows machine, but in Windows a folder gets marked by a \. Use the constant File.seperator to get the right seperator on every machine.

secustor
  • 3,001
  • 2
  • 14
  • 20