2

我正在创建一个程序来处理不同方法中不同的文本文件。就像在一种方法中打开文件流,在另一种方法中检查它是否存在,在第三种方法中读取/写入文件并在第四种方法中关闭文件流一样。

当我在检查文件是否存在的方法中使用相同的文件流时,这似乎不太顺利。

主要代码

//Imports the java.io library so the filehandler can read and write to text files.
import java.io.*;

public class filehandler    {

//Variables for the filehandler class.
public String filetohandlename;
public String filetohandleextention;
public String filetohandlefullname = filetohandlename + "." + filetohandleextention;
public String filetohandlepath;
public String filetohandle = filetohandlepath + filetohandlefullname;

//Boolean that is true if the filehandler's "openfilestream"-method has tried to open a filestream.
//Is false as long as none filestreams have been touched.
public boolean filestreamopen = false;

//Declares a variable for the filestream to access text files.
public File filestream;
//End of variable list.

//Called to open a filestream so the server can load properties from text files.
public void openfilestream(File filestream, String filetohandle)    {
    //Open a filestream called "filestream" using the variable "filetohandle"'s value
    //as information about wich file to open the filestream for.
    filestream = new File(filetohandle);
    //Turn the boolean "filestreamopen" to true so next time the server checks it's
    //value, it knows if the filehandler has tried to open a filestream.
    filestreamopen = true;
}

//Boolean that checks if the filestream exists.
public boolean filestreamexists(String filestream)  {
    //Tell the user that a check on the filestream is going on.
    System.out.println("Checking if filestream for \"" + filetohandlefullname + "\" exists...");
    //If the filestream exists...
    if(filestream.exists()) {
        //Tell the user that the filestream exists.
        System.out.println("Filestream for \"" + filetohandlefullname + "\" exists!");
        //Make the boolean's value positive.
        return true;
    }
    //If the filestream does not exist...
    else    {
        //Tell the user that the filestream does not exist.
        System.out.println("Filestream for \"" + filetohandlefullname + "\" does not exist!");
        //Make the boolean's value negative.
        return false;
    }
}

//Called to read files and collect it's information.
public void readfile(String filetohandle)   {
    //Checks if the file that is going to be read is a configuration file.
    if(filetohandleextention == "ini")  {
        //Tell the user that a configuration file is going to be read.
        System.out.println("Extracting information from the configuration file \"" + filetohandle + "\".");
    }
}
}

问题

问题在于检查文件是否存在。* if(filestream.exists()) {*
在方法“ public boolean filestreamexists(String filestream) {”中。

代码只是存储在我称为“源”的文件夹中的 .java-files 中,我正在使用批处理脚本来编译和运行代码:javac -d binary source\*.java.

我编译时得到的错误如下所示:

source\filehandler.java:36: error: cannot find symbol
                if(filestream.exists()) {
                             ^
  symbol:   method exists()
  location: variable filestream of type String

注意:“filehandler.java”不是我程序中唯一的源代码。
我还有一个名为“服务器”的课程,但如果我将它包含在这篇文章中可能会太长而且阅读起来很无聊,但是我从课程中得到了一个非常重要的部分,我觉得我不得不包括:

server.java 文件同时请求方法“openfilestream”和“filestreamexists”。这是我在调用“filestreamexists”时使用的代码:

filehandlerclass.filestreamexists(filehandlerclass.filestream);

这给了我另一个错误:

source\server.java:24: error: method filestreamexists in class filehandler canno
t be applied to given types;
                        filehandlerclass.filestreamexists(filehandlerclass.files
tream);
                                        ^
  required: String
  found: File
  reason: actual argument File cannot be converted to String by methodinvocatio
n conversation
4

1 回答 1

2

您的问题是当您声明 filestreamexists() 时。你这样声明它:

filestreamexists(String filestream) {

您将字符串传递给它而不是流尝试这样的事情:

filestreamexists(File file){

然后您需要将 if 更改为:

if(file.exists()) {

你的文件打开也有一些错误,我认为这应该更好:

public File file;
public fileOpen(String fileToOpen) {
    filexists = true;
    try {
        filestream = New File(fileToOpen);
        //File exists
    } catch (IOException e) {
        filexists = false;
        // file doesn't exist
    }
}
于 2012-11-01T09:12:03.613 回答