package javaapplication33;

import java.io.File;
import java.io.IOException;


public class JavaApplication33 {

public static void main(String[] args) throws IOException{


    File happyFile = new File("/happy.txt");
    if (happyFile.exists() == false) {
        happyFile.createNewFile();
        System.out.println("the file is created");
    } else {
        System.out.println("tHE FILE ALREADY EXSISTED   ");
     }
   }
 }


这是我的错误:

Exception in thread "main" java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:883)
at javaapplication33.JavaApplication33.main(JavaApplication33.java:14)


Java结果:1


我尝试了C://和C:/仍然无法正常工作*

最佳答案

如果需要在Windows中写入特定路径,则需要使用带引号的反斜杠:

 File happyFile = new File("c:\\mydir\\happy.txt");


要使代码通用,可以使用system properties中的路径分隔符和主目录。

10-06 07:08