假设我有一个名为“input.txt”的文件,其中包含一堆正整数:
6
5
6
8
6
2
4
等等….(每行一个整数)
我想读取这个文件并将其变成一个数组.第一个整数(在本例中为6)表示数组中索引或元素的数量,因此有6个点.其他数字从0开始填充数组.因此,在索引0处,数字为5,在索引1处,数字为6,依此类推.
有人可以告诉我如何读取这个文件并将其变成一个名为A的数组并将每个索引中的整数作为n返回吗?
这是我到目前为止:
import java.io.*;
public class inputFile {
public static jobScheduleRecursive(int[] A, int i)
{
try
{
FileReader filereader = new FileReader("input.txt");
BufferedReader bufferedreader = new BufferedReader(filereader);
String line = bufferedreader.readLine();
//While we have read in a valid line
while (line != null) {
//Try to parse integer from the String line
try {
System.out.println(Integer.parseInt(line));
} catch (NumberFormatException nfe) {
System.err.println("Failed to parse integer from line:" + line);
System.err.println(nfe.getMessage());
System.exit(1);
}
line = bufferedreader.readLine();
}
}
catch(FileNotFoundException filenotfoundexception)
{
System.out.println("File not found.");
}
catch(IOException ioexception)
{
System.out.println("File input error occured!");
ioexception.printStackTrace();
}
return A;
}
我想我做错了什么.请帮忙.