2

さまざまな方法で異なるテキスト ファイルを処理するプログラムを作成しています。ある方法でファイルストリームを開き、別の方法でファイルストリームが存在するかどうかを確認し、3 番目の方法でファイルの読み取り/書き込みを行い、4 番目の方法でファイルストリームを閉じるようにします。

ファイルが存在するかどうかを確認する方法で同じファイルストリームを使用している場合、これはうまくいかないようです。

メインコード

//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 ファイルに単純に保存されます。バッチ スクリプトを使用して、コードをコンパイルおよび実行しています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

Your problem is when you declare filestreamexists(). You declare it like this:

filestreamexists(String filestream) {

You are passing a string to it not a stream try something like this:

filestreamexists(File file){

And then you need to change the if to:

if(file.exists()) {

Also you have some errors in your fileopen, I think this should work better:

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 に答える