java 错误转义字符中的文件处理
file handling in java error escape character
正在尝试注册新用户,但登录部分已完成,只有注册时出现问题。它说非法转义字符但 运行 成功
package register;
import java.io.File;
import java.io.IOException;
public class Register {
public static void main(String[] args)
{
try{
File newUser = new File ("C:\Users\Rohani\Desktop\Filehandling\newUser.txt"); //theres an error saying illegal escape char
if (newUser.createNewFile()){
System.out.println("User created: " + newUser.getName());
}else{
System.out.println("User already exist.");
}
} catch(IOException e ){
System.out.println("An error has occured.");
}
}
您需要转义路径中的反斜杠。而不是写 \
将它们替换为 \
\
字符用于在 Java 字符串中转义。
您可以使用 \
代替 \
或使用 /
。
重复:
Windows escape sequence issue with file path in java
正在尝试注册新用户,但登录部分已完成,只有注册时出现问题。它说非法转义字符但 运行 成功
package register;
import java.io.File;
import java.io.IOException;
public class Register {
public static void main(String[] args)
{
try{
File newUser = new File ("C:\Users\Rohani\Desktop\Filehandling\newUser.txt"); //theres an error saying illegal escape char
if (newUser.createNewFile()){
System.out.println("User created: " + newUser.getName());
}else{
System.out.println("User already exist.");
}
} catch(IOException e ){
System.out.println("An error has occured.");
}
}
您需要转义路径中的反斜杠。而不是写 \
将它们替换为 \
\
字符用于在 Java 字符串中转义。
您可以使用 \
代替 \
或使用 /
。
重复:
Windows escape sequence issue with file path in java