为什么文件相关的内容无法工作?(Java)

huangapple go评论138阅读模式
英文:

Why does File related stuff not Work? (Java)

问题

package com.company;

import java.io.*;

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

    // 对于 "Data" 和 "Data/MonData.txt",它显示
    // "期望 0 个参数,但找到 1 个"

    public void DataText() {
        if(folder1.exists()) {
        }
        else {
            folder1.mkdirs();
        }
        if(file1.exists()) {
        }
        else {
            try {
                file1.createNewFile();
            }
            catch(IOException e) {
                e.printStackTrace();
            }
        }
    }
}
英文:

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();
            }
        }
    }
}

答案1

得分: 2

你应该以不同的方式命名你的类。将你的类命名为File会让Java使用它,而不是java.io.File,因此方法exists(以及其他方法)未能被找到,因为它们不在你的类中。

英文:

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.

答案2

得分: 0

你的类名和导入的类名都是相同的 File,因此编译器会检查你的 File 类,而不是应该检查的 java.io.File 类。

> 如果两个类具有相同的名称,请改为使用 java.io.Fileyour.File,而不是仅使用 File

英文:

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

答案3

得分: 0

你的两个类名相同。尝试将类名File改为java.io.File
这样应该能正常运行。

英文:

Both the classes of yours have the same name. Try naming the class File to java.io.File .
It should work fine

答案4

得分: 0

你可以使用完全限定名

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

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"); 

huangapple
  • 本文由 发表于 2020年8月24日 13:39:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63555279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定