java 在 cmd 中处理文件路径
Handling file paths in cmd in java
我正在尝试使用我的 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 代替获取路径:
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 试过,效果很好:
我正在尝试使用我的 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 代替获取路径:
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 试过,效果很好: