daxue
daxue

Reputation: 259

Why cannot create new file,is the path or the pathname separator correct?

why i can't create a new file, I wonder if it is because the path is not correct and if its not, maybe the issue is in my server.

my code:

String signaturePath = pathService.getStringByKey(myConstants.REPORT_PATH) + File.separator + "hos_log" + File.separator + params.get("driver");
            System.out.println(signaturePath);//it print this: /home/www/MyServer/report/\my_log\jo
            String fileName = shortDate + ".jpg";
            File file = new File(signaturePath + File.separator + fileName);//file debuged out : \home\www\MyServer\report\my_log\jo\20160601.jpg
            dataMap.put("shortDate", shortDate);
            dataMap.put("driverId", driverLog.getUserId());
            if (file.exists()) {//false.and i go to the sever,there is no such file created.
                dataMap.put("icon", "yes");
            }else{
                dataMap.put("icon", "no");
            }
            results.add(dataMap);

and, below is how code for the separator:

public static final String separator = "" + separatorChar;

public static final char separatorChar = fs.getSeparator();

public char getSeparator() {
return slash;
}

class Win32FileSystem extends FileSystem {

private final char slash;
private final char altSlash;
private final char semicolon;

public Win32FileSystem() {
slash = ((String) AccessController.doPrivileged(
          new GetPropertyAction("file.separator"))).charAt(0);
semicolon = ((String) AccessController.doPrivileged(
          new GetPropertyAction("path.separator"))).charAt(0);
altSlash = (this.slash == '\\') ? '/' : '\\';
}

private boolean isSlash(char c) {
return (c == '\\') || (c == '/');
}

private boolean isLetter(char c) {
return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
}

private String slashify(String p) {
if ((p.length() > 0) && (p.charAt(0) != slash)) return slash + p;
else return p;
}

I am confused about the file create, especially how to write the path.

Now i add

file.createNewFile();

right below

File file = new File(signaturePath + File.separator + fileName);

however it shut down by an Exception the exception printed in console once,but it didn't printed anymore,and the file is still not created in sever. The code before we redeployed a new server to Tomcat it is right,so I don't know how the file created physically as I have get the info from the answer of @maskacovnik

================================================================= the issue turns out that it is because the path which myConstants.REPORT_PATH is pointing to is wrong,the codes I show is no issue.but i am still confused about how it has a file even there is no

file.createNewFile();

Upvotes: 1

Views: 533

Answers (1)

maskacovnik
maskacovnik

Reputation: 3084

If you create a File object:

File file = new File(signaturePath + File.separator + fileName);

you will not create a file physicaly, that is why file.exists() returns false, this is just java object.

To create empty file use:

file.createNewFile();

Or use some streams to write in it. Also make sure you have permissions to create new file.

In case you have bad paths, it is better to use / instead of \ in paths. Windows machines can interpret it good. Your File.separator is the best choice - it should be platform independent.

In my opinion paths are ok, you just have not created the file physicaly.

Upvotes: 1

Related Questions