【问题标题】:why this is not create a backup file in specified location?为什么这不在指定位置创建备份文件?
【发布时间】:2015-11-23 19:37:48
【问题描述】:

执行此代码时,不会创建重定向中指定的文件。为什么不呢?

public class MyDdlSql {
    public static void main(String[] args) {
        try {
            Runtime.getRuntime().exec("mysqldump -uroot -psuri biztime >D:data.sql");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

【问题讨论】:

  • 看起来您正在使用 Windows。您正在使用相对路径。所以你必须查看你的当前目录(在驱动器 D 下)或使用像 D:\\data.sql 这样的绝对路径(你必须引用反斜杠,或者你可以使用正斜杠)。
  • 另外你需要以管理员权限运行程序
  • 这也不起作用......文件未创建
  • 包调度器;导入 java.io.IOException; public class MyDdlSql { public static void main(String[] args) { try { Process p=Runtime.getRuntime().exec("mysqldump -uroot -psuri biztime >D:\\data22.sql"); int i=p.waitFor(); if(i==0){ System.out.println("成功"); } else{ System.out.println("失败"); } } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } 这给出了失败和文件未在指定位置创建............

标签: java mysql


【解决方案1】:

看这里:

Runtime's exec() method is not redirecting the output

重定向是 shell 的一个特性。所以你不能使用它。相反,您应该使用 ProcessBuilder。

ProcessBuilder builder = new ProcessBuilder("mysqldump", "-uroot", "-psuri", "biztime");
builder.redirectOutput(new File("D:\\data.sql"));
builder.redirectError(new File("error.log"));
Process p = builder.start();

【讨论】:

  • 包调度器;导入 java.io.IOException; public class MyDdlSql { public static void main(String[] args) { try { Process p=Runtime.getRuntime().exec("mysqldump -uroot -psuri biztime >D:\\data22.sql"); int i=p.waitFor(); if(i==0){ System.out.println("成功"); } else{ System.out.println("失败"); } } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } 这给出了失败和文件未在指定位置创建............
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
  • 2017-02-02
  • 1970-01-01
相关资源
最近更新 更多