gpt4 book ai didi

java - 无法找出 FileReader 的问题

转载 作者:行者123 更新时间:2023-12-02 06:06:48 24 4
gpt4 key购买 nike

import java.io.File;
import java.io.FileReader;
import java.util.Scanner;

public class lab4 {

public static void main(String[] args){

/*
* Input is the name of the file and location typed by the user
* file is used as a new scanner of the file to later go into the FileReader
*/

String input;
Scanner file;

System.out.println("Please type the name of the file you wish to read into the program");

// scanner to acquire input
Scanner scanner = new Scanner(System.in);

input = scanner.nextLine();

System.out.println("the file input was " + input);

// tries to attach the specified file "input" to a new scanner "file" to later read into FileReader
try{
file = new Scanner(new File(input));
}
catch(Exception e){
System.out.println("The requested file could not be found");
}

FileReader(File file){
while(file.hasNext()){
String s = file.next();

}
}
}

}

在几个长期的编程问题之后,我在每次添加新内容后都进行了编译我在 FileReader 上遇到错误,我已经查找了示例,我所做的应该是正确的,

java: ')' expected
java: illegal start of expression
java: ';' expected
java: class, interface, or enum expected

错误指向 FileReader 的位置,所以显然我使用错误,我不需要;我看到的例子是使用 public void FileReader(File "fileName") 之类的方法我被告知将整个代码放入 public static void main(String[] args)

我看了 youtube 并查找了 API,但没有发现任何问题。

最佳答案

在执行此类操作之前,您可能应该先了解一下 Java 基础知识。

无论如何,这是一个示例:

    try
{
File file = new File("input-file.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = null;
while ( (line = bufferedReader.readLine()) != null )
{
// do stuff with the line that was read
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}

关于java - 无法找出 FileReader 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22213419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com