我正在尝试将文件作为参数发送到方法,但不知道如何操作?

I am trying to send File as a parameter to a method but dont know how?

提问人:Epitaph 提问时间:12/11/2021 更新时间:11/23/2022 访问量:584

问:

public static void createFile() {

    File file = new File("C:\\Users\\egecoskun\\Desktop\\javafiles\\ananınamı.txt");
    try {
        if (file.createNewFile()) {
            System.out.println("Dosya olusturuldu!");

        } else {
            System.out.println("Dosya zaten var!");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

这是我的createFile方法,用于创建文件。

public static void readFile(File file) {
    try {
        Scanner reader = new Scanner(file);
        while(reader.hasNextLine()) {
            String line=reader.nextLine();
            System.out.println(line);
        }
    } catch (FileNotFoundException e) {

        e.printStackTrace();
    }

}

这个方法正在读取我正在创建的文件。但是它需要将文件作为参数,但是当我尝试在Main中运行此方法时

public static void main(String[] args) {
    createFile();
    readFile(file);

}

我收到的错误是文件无法解析为变量。 有人发现我的错误吗?你能解释一下吗?

文件 异常 java.util.scanner 读取器

评论

0赞 Muhammad Ahmed 12/11/2021
从 createFile 方法创建文件对象并将其传递给 read 方法

答:

0赞 Elliott Frisch 12/11/2021 #1

createFile是;这意味着它不会返回任何内容。在本例中,您希望返回 .喜欢voidFile

public static File createFile() {
    File file = new File("C:\\Users\\egecoskun\\Desktop\\javafiles\\ananınamı.txt");
    try {
        if (file.createNewFile()) {
            System.out.println("Dosya olusturuldu!");
            return file;
        } else {
            System.out.println("Dosya zaten var!");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

然后,您需要将调用该方法的结果分配给变量。您应该在将其传递给 之前检查它是否是 .喜欢nullreadFile

File f = createFile();
if (f != null) {
    readFile(f);
}

请注意,该文件实际上不包含任何要读取的内容。它只是在你创建它之后就存在了。

评论

0赞 Epitaph 12/11/2021
感谢您的回答 Elliott,但我是否需要返回让我有点困惑的 null?
0赞 Muhammad Ahmed 12/11/2021
是的,如果目录不存在,您必须返回 null,或者可能存在,您可以检查目录是否不存在,您可以创建新目录并删除 try catch 语句
0赞 rzwitserloot 12/12/2021
请不要教新手可悲的 java 代码风格。“print the stack trace and return null”是在此代码中处理 IOExceptions 的一种疯狂方法。此方法应声明为 ,因为方法本身的签名(其名称、参数、返回类型和 javadoc)本质上意味着正在发生 I/O(因此,IOException 不是实现细节,而是其本质所固有的)。与几乎所有方法一样,该方法应声明为 。更少的代码,更好的代码,更干净的代码,更好的行为。三赢。throws IOExceptionpsv mainmainthrows Exception
0赞 Elliott Frisch 12/12/2021
@rzwitserloot 该代码在问题中,我仍然看不出读取空文件的意义。
0赞 Epitaph 12/13/2021
我没有读取空文件。我还有一个 writeFile 方法,你看不到@ElliottFrisch。
1赞 Muhammad Ahmed 12/11/2021 #2

从 createFile 函数返回文件对象并将其传递给 readFile 函数

public static File createFile() {
    File dir = new File("C:\\Users\\egecoskun\\Desktop\\javafiles");
    if(dir.exists()) {
       dir.mkdirs();
    }
    File file = new File(dir,"ananınamı.txt");
    file.createNewFile();
    return file;
}

在您的主要功能中

File file = createFile();
readFile(file);
0赞 Ishraga Mustafa Awad Allam 11/23/2022 #3
import java.io.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCall2 {
    public static void CALL(FileOutputStream fr, int w) {
       try {
         fr.write(w);
       } catch (Exception e) {
        System.out.println("Exception: " + e);
       }
    }
  public static void main(String[] args) {
     try (FileInputStream f = new FileInputStream("c:\\code\\a.txt")) {
        FileOutputStream f2 = new FileOutputStream("c:\\code\\a2.txt");
      int c;
      char wd;
      while((c = f.read()) != -1) {
          System.out.print((char)c);
          wd = (char)c;
          CALL(f2, c);
          }
      f.close();              
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
    }
}