java - 在java中处理cmd中的文件路径
问题描述
我正在尝试使用我的 java 程序从一个文件夹中复制一个文件并通过命令行粘贴到另一个文件夹中,但我遇到了一堆不同的错误。我的代码是
public static void main(String[] args) throws IOException
{
String src = args[0];
String dest = args[1];
String temp[] = src.split("\\");
String fileName = temp[temp.length-1];
String data;
FileReader fr = null;
BufferedReader br = null;
FileWriter fw = null;
PrintWriter pw = null;
try
{
fr = new FileReader(src);
br = new BufferedReader(fr);
fw = new FileWriter(dest + "\\" + fileName);
pw = new PrintWriter(fw);
data = br.readLine();
while(data != null)
{
pw.println(data);
data = br.readLine();
}
}
catch(IOException ex)
{
System.out.println(ex.getMessage());
}
}
请告诉我我的代码做错了什么。提前致谢。
这些是我在 cmd https://imgur.com/a/83JvVmP上遇到的错误
解决方案
您可以使用 Paths 来获取 de 路径:
public static void main(String[] args) {
String src = args[0];
String dest = args[1];
String fileName = Paths.get(src).getFileName().toString();
System.out.println("fileName:" + fileName);
String data;
FileReader fr = null;
BufferedReader br = null;
FileWriter fw = null;
PrintWriter pw = null;
try {
fr = new FileReader(src);
br = new BufferedReader(fr);
fw = new FileWriter(dest + "\\" + fileName);
pw = new PrintWriter(fw);
data = br.readLine();
while (data != null) {
pw.println(data);
data = br.readLine();
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
我在 Eclipse 中尝试了这个,并使用了一个 Jar,并且工作正常:
推荐阅读
- html - 波纹效果动画在我的 HTML 代码中不起作用
- c# - 如何从树视图上的 HierarchicalDataTemplate 中删除项目
- node.js - Firebase 中的身份验证问题 - 为什么我的令牌会立即过期?
- node.js - 您如何以编程方式退出事件中的 node.js/express 路由?
- typescript - 从类型联合定义打字稿函数签名
- sql - 如何在 BigQuery 中取消嵌套和旋转两列
- html - 使用 fontawesome css 格式化
- angular - PrimeNG:单击 p-dropdown 的 li 元素
- python - 在 docker 镜像中使用 python 和 python 模块安装
- azure - AKS Kube 代理不公开指标