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(文件"文件名")之类的方法有人告诉我将我的整个代码放在公共静态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();
}
更好的编码实践可能是在Main()方法之外定义局部变量input
和file
...
public static String input;
像这样...
public static Scanner file;
和这个...
尽管该程序仍将起作用,因为该代码在使用前初始化了这些本地变量。我认为这可能是一个问题,因为有时编译器可能很难解释。然而,只要使用static
修饰符与main()的静态上下文一起使用,在MAIN之外声明这些变量也不会受到伤害。
Java不会自动初始化局部变量。如果未在方法中使用之前,则可能会导致错误。
也是FileReader是一类,不能以与方法相同的方式使用。首先,应实例化fileReader对象。之后,您可以在对象引用上调用方法,并通过参考将fileReader对象上的成员字段状态更改。
根据Java 7 API,您可以通过3种方式实例化Java.io.filereader对象。一种方法采用文件对象,另一个方法取一个字符串对象,而另一个则采用我不熟悉的不同类型的对象。
因此,例如,您可以像这样实例化fileReader:
FileReader fR = new FileReader("myfile.csv");
或
FileReader fR2 = new FileReader(new File("myOtherFile.txt"));
您有一段时间时阅读此文档:http://docs.oracle.com/javase/7/docs/api/java/io/file.html
另外,请查看这些人在文件中阅读时的代码:http://www.mkyong.com/java/how-to-to-read-file-file-from-java-bufferedreader-example/
最后,我编辑了您的程序以读取文件:
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
import java.io.*;
public class lab4 {
static BufferedReader br = null;
public static void main(String[] args){
String s = null;
/*
* 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{
// wrap the FileReader object instantiated from the input variable in a
// BufferedReader object
br = new BufferedReader(new FileReader(input));
// read each line to the console in this while loop that runs as long as it does not equal null
while((s = br.readLine()) != null){
System.out.println(s);
}
}
catch(Exception e){
System.out.println("The requested file could not be found");
}
}
}
愉快的编码!
请告诉我这是否有效。