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而没有骰子。
答案 0 :(得分:1)
更好的编码实践可能是在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对象。 一种方式采用File对象,另一种采用String对象,另一种采用我不熟悉的不同类型的对象。
例如,您可以像这样实例化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-read-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");
}
}
}
快乐的编码!
如果有效,请告诉我。
答案 1 :(得分:1)
在执行此类操作之前,您应该完成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();
}