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

2024-11-19 10:41:02
推荐回答(2个)
回答1:

源代码如下,自己运行下:

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 x = new ArrayList();
static List y = new ArrayList();
static List z = new ArrayList();

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:

都一样 读取文本文件 最好用readLine 读完之后对每个字符串分割处理