菜鸟求解答,为什么我从文件中读取数组后做了个输出,结果全部是0呢

qq_36173067 2017-03-13 05:00:36

public static void main(String arg[]) throws IOException{
File file=new File("D:/java/hasj","input.txt");
String line;
int arr1[]=new int[100000];
String temp[];
BufferedReader in = new BufferedReader(new FileReader(file));
while((line = in.readLine()) != null){
temp = line.split("\n");

for(int j=0;j<temp.length;j++){
arr1[j]= Integer.parseInt(temp[j]);
//System.out.println("shuzu is "+arr1[j]);
}

}
in.close();
for(int i=0;i<100000;i++){
System.out.println(arr1[i]);
}

}


...全文
164 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_36173067 2017-03-13
  • 打赏
  • 举报
回复
引用 3 楼 qq_30831935 的回复:
[quote=引用 2 楼 qq_36173067 的回复:] [quote=引用 1 楼 qq_30831935 的回复:] 我测试了你的代码 没毛病 文件读出来的可以打印出来 后面的0 可能是int初始化赋值0
可是我想让它读出txt文件中的数据啊,int数据放在arr1数组中,就是没有办法实现[/quote] 没太懂你意思,提供一种集合的办法
public static void main(String arg[]) throws IOException {
		File file = new File("D:", "test.txt");
		String line;
		List<Integer> list = new ArrayList<>();
		String temp[];
		BufferedReader in = new BufferedReader(new FileReader(file));
		while ((line = in.readLine()) != null) {
			temp = line.split("\n");
			for (int j = 0; j < temp.length; j++) {
				list.add(Integer.parseInt(temp[j]));
				 System.out.println("shuzu is "+temp[j]);
			}
		}
		in.close();
		System.out.println(list);
//		for(int i=0;i<100000;i++){
//			System.out.println(list);
//			}
	}
[/quote] 就是我想把txt文件中的数据读出来写入一个数组中,但是窝在测试数组打印出来全部都是0啊,不知道为什么
鱿鱼ing 2017-03-13
  • 打赏
  • 举报
回复
引用 2 楼 qq_36173067 的回复:
[quote=引用 1 楼 qq_30831935 的回复:] 我测试了你的代码 没毛病 文件读出来的可以打印出来 后面的0 可能是int初始化赋值0
可是我想让它读出txt文件中的数据啊,int数据放在arr1数组中,就是没有办法实现[/quote] 没太懂你意思,提供一种集合的办法
public static void main(String arg[]) throws IOException {
		File file = new File("D:", "test.txt");
		String line;
		List<Integer> list = new ArrayList<>();
		String temp[];
		BufferedReader in = new BufferedReader(new FileReader(file));
		while ((line = in.readLine()) != null) {
			temp = line.split("\n");
			for (int j = 0; j < temp.length; j++) {
				list.add(Integer.parseInt(temp[j]));
				 System.out.println("shuzu is "+temp[j]);
			}
		}
		in.close();
		System.out.println(list);
//		for(int i=0;i<100000;i++){
//			System.out.println(list);
//			}
	}
qq_36173067 2017-03-13
  • 打赏
  • 举报
回复
引用 1 楼 qq_30831935 的回复:
我测试了你的代码 没毛病 文件读出来的可以打印出来 后面的0 可能是int初始化赋值0
可是我想让它读出txt文件中的数据啊,int数据放在arr1数组中,就是没有办法实现
鱿鱼ing 2017-03-13
  • 打赏
  • 举报
回复
我测试了你的代码 没毛病 文件读出来的可以打印出来 后面的0 可能是int初始化赋值0

51,412

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧