简体   繁体   中英

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.

Your class name & importing class has same name File , So compiler check for your File class not java.io.File one which he should.

In case two class had same name, use java.io.File & your.File instead of File only

Both the classes of yours have the same name. Try naming the class File to 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"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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