Java中通过文本文件输入的DNA数据
我们有一个小组项目,从用户输入的文本文件中获取数据。我们已经创建了一个方法来获取输入,并将数据保存到输出文件中,但我仍然无法确定如何读取文件,因为我们创建的方法将读取整个文件。目前它只是打印到控制台Java中通过文本文件输入的DNA数据,java,arrays,user-input,Java,Arrays,User Input,我们有一个小组项目,从用户输入的文本文件中获取数据。我们已经创建了一个方法来获取输入,并将数据保存到输出文件中,但我仍然无法确定如何读取文件,因为我们创建的方法将读取整个文件。目前它只是打印到控制台 public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner(new File("dna.txt")); //create scanner object for
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
}
}
}
这让我很困惑。通过字符串inputFile看起来还可以,但是while循环在in参数上运行,该参数不是inputFile变量,我想你认为它是输出文件的文件名?。好的,是的,这就是我认为我做错了但无法判断的地方。我只是有一个困难的时间,因为没有while循环,它只是将我的txt文件打印到控制台,但我需要它被我稍后调用的方法读取。要读取该文件,谢谢大家,我们都解决了!