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 

```
['https://Stackoverflow.com/questions/20066413', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1966049/']
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);

```
Since Java 7, you can use the [Files](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html) utility class, with the new [Path](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html) 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.