1

Here is what I wanna do:

  1. Check if a folder exists
  2. If it does not exists, create the folder
  3. If it doest exists do nothing
  4. At last create a file in that folder

Everything is working fine in Windows 7, but when I run the application in Ubuntu it doesn't create the folder, it is just creating the file with the folder name, example: (my file name is xxx.xml and the folder is d:\temp, so in Ubuntu the file is generated at d: with the name temp\xxx.xml). Here is my code:

File folder = new File("D:\\temp");
if (folder.exists() && folder.isDirectory()) {
} else {
    folder.mkdir();
}

String filePath = folder + File.separator;
File file = new File(filePath + "xxx.xml");

StreamResult result = new StreamResult(file);
transformer.transform(source, result);
// more code here 
Shahe Masoyan
  • 157
  • 4
  • 4
  • 14

7 Answers7

3

You directory (D:\temp) is nos appropriate on Linux.

Please, consider using linux File System, and the File.SEPARATOR constant :

static String OS = System.getProperty("OS.name").toLowerCase();
String root = "/tmp";

if (OS.indexOf("win") >= 0) {
    root="D:\\temp";
} else {
    root="/";
}

File folder = new File(ROOT + "dir1" + File.SEPARATOR + "dir2");

if (folder.exists() && folder.isDirectory()) {
} else {
    folder.mkdir();
}

Didn't tried it, but whould work.

Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
  • 3
    Nope. If you create a folder to `D:\something` linux creates a folder named `D:\something` in your user directory (tried it) – BackSlash Nov 19 '13 at 08:22
  • I will be more accurate, but I meany "D:\temp", I didn't see Shase doubled it. You're right it works, I always complain about my colleagues that left thos kind of path in log4j configuration :) – Jean-Rémy Revy Nov 19 '13 at 08:25
  • `root="/";` It not good solution because not each user can write to `root`. More correct write to `/tmp` or home directory. – Michael Kazarian Nov 19 '13 at 08:42
3

Linux does not use drive letters (like D:) and uses forward slashes as file separator.

You can do something like this:

File folder = new File("/path/name/of/the/folder");
folder.mkdirs(); // this will also create parent directories if necessary
File file = new File(folder, "filename");
StreamResult result = new StreamResult(file);
Henry
  • 42,982
  • 7
  • 68
  • 84
2

D:\temp does not exists in linux systems (what I mean is it interprets it as if it were any other foldername)

In Linux systems the file seperator is / instead of \ as in case of Windows

so the solution is to :

File folder = new File("/tmp"); 

instead of

File folder = new File("D:\\temp");
codeMan
  • 5,730
  • 3
  • 27
  • 51
1

On Unix-like systems no logical discs. You can try create on /tmp or /home Below code for create temp dirrectory in your home directory:

String myPathCandidate = System.getProperty("os.name").equals("Linux")? System.getProperty("user.home"):"D:\\";
  System.out.println(myPathCandidate);
  //Check write permissions
  File folder = new File(myPathCandidate);
  if (folder.exists() && folder.isDirectory() && folder.canWrite()) {
      System.out.println("Create directory here");
  } else {System.out.println("Wrong path");}

or, for /tmp - system temp dicecrory. Majority of users can write here:

String myPathCandidate = System.getProperty("os.name").equals("Linux")? System.getProperty("java.io.tmpdir"):"D:\\";
Michael Kazarian
  • 4,376
  • 1
  • 21
  • 25
1

Before Java 7 the File API has some possibilities to create a temporary file, utilising the operating system configuration (like temp files on a RAM disk). Since Java 7 use the utility functions class Files.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

Consider both solutions using the getProperty static method of System class.

String os = System.getProperty("os.name");

if(os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0 || os.indexOf("aix") > 0 ) // Unix
    File folder = new File("/home/tmp"); 
else if(os.indexOf("win") >= 0) // Windows
    File folder = new File("D:\\temp");
else
    throw Exception("your message");
1

Since Java 7, you can use the Files utility class, with the new Path class. Note that exception handling has been omitted in the examples below.

// uses os separator for path/to/folder.
Path file = Paths.get("path","to","file");

// this creates directories in case they don't exist
Files.createDirectories(file.getParent());

if (!Files.exists(file)) {
    Files.createFile(file);
}

StreamResult result = new StreamResult(file.toFile());
transformer.transform(source, result);

this covers the generic case, create a folder if it doesn't exist and a file on that folder.


In case you actually want to create a temporary file, as written in your example, then you just need to do the following:

// this create a temporary file on the system's default temp folder.
Path tempFile = Files.createTempFile("xxx", "xml");

StreamResult result = new StreamResult(Files.newOutputStream(file, CREATE, APPEND, DELETE_ON_CLOSE));
transformer.transform(source, result);

Note that with this method, the file name will not correspond exactly to the prefix you used (xxx, in this case).

Still, given that it's a temp file, that shouldn't matter at all. The DELETE_ON_CLOSE guarantees that the file will get deleted when closed.

Tarek
  • 3,080
  • 5
  • 38
  • 54