如何把文本文件中的数据读到数组中 如文本中的数据为: 111213读到数组中为: a[0] : 11 a[1] : 12a[2] : 13用JAVA实现,谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 从文本里读取 那就用IO流然后用for循环添加到数组里!LZ 还是自己动手吧! 写过一遍 感觉就不一样了PS: LZ 及时结贴! 先可以在外面定义一个数组,再用bufferedRead流一行一行循环读取就就好了,然后在循环中把读取到的值赋值给数组 public void readTxt() { FileReader file; try { file = new FileReader("D:/data.txt"); BufferedReader in = new BufferedReader(file); while (in.ready()) { array[i] = in.readline(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }给出一个框架,楼主参考。。 import java.io.File;import java.io.BufferedReader;import java.io.FileReader;public class Ansewer16{ public static void main(String[] ags) throws Exception { BufferedReader br=new BufferedReader(new FileReader(new File("c:"+File.separator+"11.txt"))); int[] a=new int[3]; int n=0; String s=""; while((s=br.readLine())!=null) { a[n++]=Integer.parseInt(s); } for(int in:a) System.out.println(in); }} import java.io.*;import java.util.ArrayList;import java.util.List;public class Test{ public static void main(String [] args)throws Exception{ List<String> sList = new ArrayList<String>(); File file = new File("你的文件所在路径");//文件 BufferedReader br = new BufferedReader(new FileReader(file)); String s = null; while((s=br.readLine())!=null)//读文件 sList.add(s); int arr[] = new int[sList.size()];//创建数组 for(int i=0;i<arr.length;i++){ arr[i] = Integer.parseInt(sList.get(i));//给数组赋值 } for(int t : arr)//输出结果 System.out.println(t); br.close(); System.out.println("请楼主给分吧!"); }} synchronized和Lock 差别? 敢不敢有人留个言???T 。T 菜鸟啊啊!!我是。。。各位大神。。。帮帮忙好吧。。我真的不会 求一个字符串算法 新手问题 byte的一个问题 向高手请教一个问题! (Struts)多选multibox验证validation.xml该如何写 请教:如何把字符'2'转换成数字2? 急,关于URL类和HttpConnection类的问题,急在线等待!!! 网络编程问题,如何通过代理服务器收发短信???? "a|b".split("|").length 听说jdk1.6对线程池有个什么优化来着,有人知道么?
然后用for循环添加到数组里!LZ 还是自己动手吧! 写过一遍 感觉就不一样了PS: LZ 及时结贴!
FileReader file;
try {
file = new FileReader("D:/data.txt");
BufferedReader in = new BufferedReader(file);
while (in.ready()) {
array[i] = in.readline();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}给出一个框架,楼主参考。。
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
public class Ansewer16
{
public static void main(String[] ags) throws Exception
{
BufferedReader br=new BufferedReader(new FileReader(new File("c:"+File.separator+"11.txt")));
int[] a=new int[3];
int n=0;
String s="";
while((s=br.readLine())!=null)
{
a[n++]=Integer.parseInt(s);
}
for(int in:a)
System.out.println(in);
}
}
import java.util.ArrayList;
import java.util.List;public class Test{
public static void main(String [] args)throws Exception{
List<String> sList = new ArrayList<String>();
File file = new File("你的文件所在路径");//文件
BufferedReader br = new BufferedReader(new FileReader(file));
String s = null;
while((s=br.readLine())!=null)//读文件
sList.add(s);
int arr[] = new int[sList.size()];//创建数组
for(int i=0;i<arr.length;i++){
arr[i] = Integer.parseInt(sList.get(i));//给数组赋值
}
for(int t : arr)//输出结果
System.out.println(t);
br.close();
System.out.println("请楼主给分吧!");
}
}