//在指定的目录下创建单个文件
package chapter7;
import java.io.*;
import java.lang.*;
public class FileDemo_03 {
public static boolean creatFile(String destFileName){
File file=new File(destFileName);
if(file.exists()){
System.out.println("创建单个文件"+destFileName+"失败,目标文件已存在");
return false;
}
//如果传入的文件名是以文件分隔符结尾的,则说明此File对象是个目录而不是文件
if(destFileName.endsWith(File.separator)){
System.out.println("创个单个文件"+destFileName+"失败,目标文件不能为目录");
return false;
}
//判断目标文件所在的目录是否存在
if(!file.getParentFile().exists()){
System.out.println("创建"+file.getName()+"所在目录不存在,正在创建!");
//判断父文件夹是否存在,如果存在则表示创建成功否则不成功
if(!file.getParentFile().mkdirs()){
System.out.println("创建目标文件所在的目录失败");
return false;
}
}
try{    //调用creatNewFile()方法,判断创建指定文件是否成功
if(file.createNewFile()){
System.out.println("创建单个文件"+destFileName+"成功!");
return ture;
}else{
System.out.println("创建单个文件"+destFileName+"失败!");
return false;
}
}catch(IOException e){
e.printStackTrace();
System.out.println("创建文件"+destFileName+"失败!"+e.getMessage());
return false;
}
}
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建目录
String dirName="D:/temp/aa";
//创建文件
String fileName=dirName+"/bb/ccFile.txt";
FileDemo_03.createFile(fileName);
}}编译报错:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method createFile(String) is undefined for the type FileDemo_03 at chapter7.FileDemo_03.main(FileDemo_03.java:49)
求高人指点啊。

解决方案 »

  1.   


    import java.io.*;
    import java.lang.*;public class FileDemo_03 {
        public static boolean creatFile(String destFileName) {
    File file = new File(destFileName);
    if (file.exists()) {
        System.out.println("创建单个文件" + destFileName + "失败,目标文件已存在");
        return false;
    }
    // 如果传入的文件名是以文件分隔符结尾的,则说明此File对象是个目录而不是文件
    if (destFileName.endsWith(File.separator)) {
        System.out.println("创个单个文件" + destFileName + "失败,目标文件不能为目录");
        return false;
    }
    // 判断目标文件所在的目录是否存在
    if (!file.getParentFile().exists()) {
        System.out.println("创建" + file.getName() + "所在目录不存在,正在创建!");
        // 判断父文件夹是否存在,如果存在则表示创建成功否则不成功
        if (!file.getParentFile().mkdirs()) {
    System.out.println("创建目标文件所在的目录失败");
    return false;
        }
    }
    try { // 调用creatNewFile()方法,判断创建指定文件是否成功
        if (file.createNewFile()) {
    System.out.println("创建单个文件" + destFileName + "成功!");
    return true; // 这里返回值写错了这里返回值写错了这里返回值写错了这里返回值写错了这里返回值写错了
        } else {
    System.out.println("创建单个文件" + destFileName + "失败!");
    return false;
        }
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("创建文件" + destFileName + "失败!" + e.getMessage());
        return false;
    }
        }    /**
         * @param args
         */
        public static void main(String[] args) {
    // TODO Auto-generated method stub
    //创建目录
    String dirName = "D:/temp/aa";
    //创建文件
    String fileName = dirName + "/bb/ccFile.txt";
    FileDemo_03.creatFile(fileName);//这里函数名也写错了,这里函数名也写错了,这里函数名也写错了,这里函数名也写错了,这里函数名也写错了,
        }}
      

  2.   

    public static boolean creatFile(String destFileName){
    楼主少个e吧
      

  3.   

    根据 The method createFile(String) is undefined 这个错误提示,可以知道,createFile(String)没有这样的方法,你去看看你定义的方法是不是create少了个e,仔细看看错误提示,可以发现问题的所在的。认真点!!
      

  4.   


    返回值是写的return ture啊
    最后那个函数名改成createFile还是不行
      

  5.   

    System.out.println("创建单个文件"+destFileName+"成功!");
    return ture;这个地方true写错了,改过来就好了