0

即使在提及路径之后,我在提及路径时也会出现以下'\'错误

File file = new File("E:\\RConnect4s\\RConnect4s\\ZoneEast\\Utilities\\NBR.txt");

我的完整代码

public class UnixInterface implements Serializable {


String content = "This is the content to write into file";

/*File file = new File("E:\\RConnect4s\\RConnect4s\\ZoneEast\\Utilities\\NBR.txt");*/
File file = new File("E:\\RConnect4s\\RConnect4s\\ZoneEast\\Utilities\\NBR.txt");

    if (!file.exists()) {
    file.createNewFile();
}

FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();

System.out.println("Done");

}
4

1 回答 1

1

您的代码工作正常查看您的代码

有关更多详细信息,请参阅来自Java Doc


更新:在您的代码中,您只能在类级别进行初始化,因此请尝试在任何函数内进行操作,例如:

public class UnixInterface implements Serializable {

  public static void main(String[] args) {
     String content = "This is the content to write into file";    
      /*File file = new File("E:\\RConnect4s\\RConnect4s\\ZoneEast\\Utilities\\NBR.txt");*/
      File file = new File("E:\\RConnect4s\\RConnect4s\\ZoneEast\\Utilities\\NBR.txt");

       if (!file.exists()) {
           file.createNewFile();
       }  

      FileWriter fw = new FileWriter(file.getAbsoluteFile());
      BufferedWriter bw = new BufferedWriter(fw);
      bw.write(content);
      bw.close();    
      System.out.println("Done");
   }
}

此代码也有效,请参阅您的代码 2

于 2013-10-31T06:29:54.910 回答