简体   繁体   English

为什么文件相关的东西不起作用? (爪哇)

[英]Why does File related stuff not Work? (Java)

Im currently writing a program which involves creating a Folder and a File within this Folder.我目前正在编写一个程序,该程序涉及在该文件夹中创建一个文件夹和一个文件。 The first version worked, after that i decided to create a new project to give the code a clear form.第一个版本有效,之后我决定创建一个新项目来为代码提供清晰的形式。 Now, suddenly the class creating the Files wont work anymore.现在,突然创建文件的类将不再工作。 I switched devices with the second project.我用第二个项目切换了设备。

package com.company;

import java.io.*;

public class File {
    File folder1 = new File("Data");
    File file1 = new File("Data/MonData.txt"); 

    //For both "Data" and "Data/MonData.txt it says 
    //"Expected 0 arguments but found 1"

    public void DataText() {
        if(folder1.exists()) {         //exists = cant
        }                              //resolve method
        else {
            folder1.mkdirs();          //mkdirs = cant
        }                              //resolve method
        if(file1.exists()) {           //exists = cant
        }                              //resolve method
        else {
            try {
                file1.createNewFile(); //createNewFile = cant 
            }                          //resolve method
            catch(IOException e) {
                e.printStackTrace();
            }
        }
    }
}

You should name your class in a different way.你应该以不同的方式命名你的类。 Naming your class File let java use it instead of java.io.File, so the method exists (and so the others) is not found because not in your class.命名您的类File让 java 使用它而不是 java.io.File,因此找不到该方法exists (以及其他方法),因为不在您的类中。

Your class name & importing class has same name File , So compiler check for your File class not java.io.File one which he should.您的类名和导入类具有相同的名称File ,因此编译器检查您的File类,而不是他应该检查的java.io.File类。

In case two class had same name, use java.io.File & your.File instead of File only如果两个类具有相同的名称,请使用java.io.File & your.File而不是File only

Both the classes of yours have the same name.你的两个班级同名。 Try naming the class File to java.io.File .尝试将类File命名为java.io.File It should work fine它应该可以正常工作

You can use fully qualified name您可以使用完全限定名称

java.io.File folder1 = new java.io.File("Data");
java.io.File file1 = new java.io.File("Data/MonData.txt"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM