java读取文件中的数组中_Java:从文件中读取整数数组

假设我有一个名为“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;

}

我想我做错了什么.请帮忙.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值