【问题标题】:Declaring a filestream in one method, checking in another?以一种方法声明文件流,签入另一种方法?
【发布时间】:2012-11-01 08:43:36
【问题描述】:

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

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

主要代码

//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

【问题讨论】:

    标签: java filestream


    【解决方案1】:

    您的问题是当您声明 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
        }
    }
    

    【讨论】:

    • 谢谢!我按照您的要求更改了源代码并消除了错误!现在我的编译器接受 if 语句来检查文件是否存在!我还从第 24 行的“server.java”中删除了错误,但我得到了一个新错误,而不是说“找不到符号”指向参数(“Filestream”文件流)。我将您对“Filestream”的建议更改为“File”并摆脱了新错误。谢谢!
    • 别忘了我的新编辑,我在你的 fileopen 函数中看到了一些错误供你更改
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 2017-10-30
    • 2013-02-10
    相关资源
    最近更新 更多