我们有一个小组项目,用于从用户输入的文本文件中获取数据。我们已经创建了一个方法来获取输入并将数据保存到输出文件中,但我仍然不知道如何为我们创建的方法读取文件,然后读取文件。目前它只是打印到控制台。
public static void main(String[] args) throws FileNotFoundException {
Scanner in = new Scanner(new File("dna.txt")); //create scanner object for dna.txt
processFile(in);//call the method using scanner as parameter
File outputFile = new File("DataOutput.txt");
PrintStream fileOutput = new PrintStream(outputFile);
fileOutput.close();
}
public static void processFile(Scanner in) throws FileNotFoundException{
Scanner console = new Scanner(System.in);//open Scanner in the console
System.out.println("File to be read: ");//prompt user to enter the txt file
String inputFile = console.next();//turns file into string and read in console
while (in.hasNextLine()){//while stuff is in file, keep looping
String s = in.nextLine();//contents in file = string s. we can now process s with arrays
System.out.println(s);//print s
}
}
}