【问题标题】:Why cannot create new file,is the path or the pathname separator correct?为什么无法创建新文件,路径或路径名分隔符是否正确?
【发布时间】:2016-10-18 22:31:47
【问题描述】:

为什么我不能创建一个新文件,我想知道是不是因为路径不正确,如果不是,可能问题出在我的服务器上。

我的代码:

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

并且,下面是分隔符的代码:

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

我对文件创建感到困惑,尤其是如何编写路径。

现在我添加

file.createNewFile();

正下方

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

但是它被异常关闭了,控制台打印了一次异常,但它不再打印了,并且文件仍然没有在服务器中创建。 我们将新服务器重新部署到 Tomcat 之前的代码是正确的,所以我不知道文件是如何物理创建的,因为我从@maskacovnik 的答案中获得了信息

================================================ =================== 问题原来是因为 myConstants.REPORT_PATH 指向的路径是错误的,我显示的代码没有问题。但即使没有文件,我仍然对它如何拥有文件感到困惑

file.createNewFile();

【问题讨论】:

  • 这是我同事的代码,他不在这里工作,我是新来分配这个代码的,不知道为什么存在无法创建文件的问题
  • 如果没有例外,我帮不了你。如果出现,请附加日志。这很奇怪,因为如果没有异常,就应该创建它。

标签: java path slash


【解决方案1】:

如果你创建一个 File 对象:

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

你不会在物理上创建文件,这就是为什么file.exists() 返回false,这只是 java 对象。

要创建空文件,请使用:

file.createNewFile();

或者使用一些流在其中写入。还要确保您有权创建新文件。

如果您有错误的路径,最好在路径中使用/ 而不是\。 Windows 机器可以很好地解释它。您的 File.separator 是最佳选择 - 它应该独立于平台。

在我看来路径没问题,只是你还没有物理创建文件。

【讨论】:

  • 是的,确定 `\home\www\MyServer\report\my_log\jo` 文件夹是否存在 @JOLee
  • 当我添加一行 file.createNewFile();出现异常。
  • 所以请编辑您的问题并在此处添加例外@JOLee
  • 谢谢@maskacovnik,问题原来是因为 myConstants.REPORT_PATH 指向的路径是错误的,我显示的代码没有问题。但我仍然对它的方式感到困惑一个文件,即使没有 file.createNewFile();
猜你喜欢
  • 2012-02-16
  • 1970-01-01
  • 2015-02-09
  • 1970-01-01
  • 2018-09-10
  • 1970-01-01
  • 2016-04-25
  • 2014-06-27
  • 1970-01-01
相关资源
最近更新 更多