Shahe Masoyan
Shahe Masoyan

Reputation: 177

Create a folder and a file with Java in Ubuntu

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 

Upvotes: 1

Views: 16097

Answers (7)

Tarek
Tarek

Reputation: 3160

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.

Upvotes: 1

Michael Kazarian
Michael Kazarian

Reputation: 4462

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:\\";

Upvotes: 1

Jean-Rémy Revy
Jean-Rémy Revy

Reputation: 5677

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.

Upvotes: 3

codeMan
codeMan

Reputation: 5758

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");

Upvotes: 2

Alejandro Ruiz Arias
Alejandro Ruiz Arias

Reputation: 259

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");

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109547

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.

Upvotes: 1

Henry
Henry

Reputation: 43728

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);

Upvotes: 3

Related Questions