Java和Windows - 错误:非法转义字符

use*_*989 3 java windows file-io

我已经完成了更改注册表数据的.java文件.但是我在Runtime.getRuntime().exec存在的行上遇到"非法转义字符"错误.我的错误在哪里?

import java.util.*;
import java.applet.Applet; 
import java.awt.*; 

class test {
  public static void main(String args[]) {
      try {
          Runtime.getRuntime().exec("REG ADD 'HKCU\Software\Microsoft\Internet Explorer\Main' /V 'Start Page' /D 'http://www.stackoverflow.com/' /F");
      } catch (Exception e) {
          System.out.println("Error ocured!");
      }
  }
}
Run Code Online (Sandbox Code Playgroud)

jah*_*roy 16

您需要转义路径中使用的反斜杠.

String windowsPath = "\\Users\\FunkyGuy\\My Documents\\Hello.txt";
Run Code Online (Sandbox Code Playgroud)


Bhe*_*ung 5

您需要\使用 another转义\,因此在您的输入字符串中替换\with \\