Java读取TXT文档中的数据并赋值给动态数组

0 0.5 100
0 0.8 200
1 0.9 230
。。。
还有很多数据,在JAVA中怎样把这个TXT文档中的数据分别读到X,Y,Z三个动态数组

VB中很简单,只要
CommonDialog1.Filter = "*.txt(文本文件)|*.txt"
CommonDialog1.ShowOpen
Df = CommonDialog1.FileName
Open Df For Input As #11
k = 1 '定义K为动态数组计数器
Do While Not EOF(11)
ReDim Preserve XAxis(k), YAxis(k), ZAxis(k)
Input #11, XAxis(k), YAxis(k), ZAxis(k)
k = k + 1
Loop
Close #11

请高手指点下在JAVA中怎么操作?(希望有代码供参考)
因为有很多数据所以只给出部分,但希望把这三行数据也做成动态处理添加到数组中,谢谢!

第1个回答  推荐于2016-09-17
源代码如下,自己运行下:

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

public class Test extends JFrame
{

static List<Double> x = new ArrayList<Double>();
static List<Double> y = new ArrayList<Double>();
static List<Double> z = new ArrayList<Double>();

public static void main(String[] args) throws Exception
{
BufferedReader in = new BufferedReader(new FileReader("1.txt"));// 这里"1.txt"是文件名,因此要按实际情况修改
String ss = "";
while ((ss = in.readLine()) != null)
{
String[] s = ss.split(" ");
x.add(Double.parseDouble(s[0]));
y.add(Double.parseDouble(s[1]));
z.add(Double.parseDouble(s[2]));
}

System.out.println(x);
System.out.println(y);
System.out.println(z);

in.close();
}

}本回答被提问者采纳
第2个回答  2010-10-12
都一样 读取文本文件 最好用readLine 读完之后对每个字符串分割处理
相似回答