java 怎样将TXT文件里的数值读入一个数组里
txt里的内容只有一行:0.523.545中间是空格相隔,要将这串数字存入一个数组中a[0]=0.5a[1]=1......
txt里的内容只有一行:0.5 2 3.5 4 5 中间是空格相隔,要将这串数字存入一个数组中
a[0]=0.5
a[1]=1
... 展开
a[0]=0.5
a[1]=1
... 展开
展开全部
// 直接给一个只读一行的吧,呵呵,数字之间以空格来分隔
public static void main(String[] args) throws Exception {
File file = new File("src/test.txt");
double[] a = getLineFromTxt(file," ");
for(int i = 0; i< a.length; i++){
System.out.println("a["+i+"] = "+a[i]);
}
}
test中的数据以空格隔开 12 32 32.02
public static double[] getLineFromTxt(File file, String split) throws Exception{
BufferedReader br = new BufferedReader(new FileReader(file));
String firstLine = br.readLine(); //就读第一行哦
String[] arrs = firstLine.split(" ");
//将字符数组转为double数组
double[] arr = new double[arrs.length];
for(int i = 0; i< arr.length; i++){
arr[i] = Double.parseDouble(arrs[i]);
}
if(br!= null){
br.close();
br = null;
}
return arr;
}
展开全部
// 直接给一个只读一行的吧,呵呵,数字之间以空格来分隔
public static void main(String[] args) throws Exception {
File file = new File("src/test.txt");
double[] a = getLineFromTxt(file," ");
for(int i = 0; i< a.length; i++){
System.out.println("a["+i+"] = "+a[i]);
}
}
public static double[] getLineFromTxt(File file, String split) throws Exception{
BufferedReader br = new BufferedReader(new FileReader(file));
String firstLine = br.readLine(); //就读第一行哦
String[] arrs = firstLine.split(" ");
//将字符数组转为double数组
double[] arr = new double[arrs.length];
for(int i = 0; i< arr.length; i++){
arr[i] = Double.parseDouble(arrs[i]);
}
if(br!= null){
br.close();
br = null;
}
return arr;
}
public static void main(String[] args) throws Exception {
File file = new File("src/test.txt");
double[] a = getLineFromTxt(file," ");
for(int i = 0; i< a.length; i++){
System.out.println("a["+i+"] = "+a[i]);
}
}
public static double[] getLineFromTxt(File file, String split) throws Exception{
BufferedReader br = new BufferedReader(new FileReader(file));
String firstLine = br.readLine(); //就读第一行哦
String[] arrs = firstLine.split(" ");
//将字符数组转为double数组
double[] arr = new double[arrs.length];
for(int i = 0; i< arr.length; i++){
arr[i] = Double.parseDouble(arrs[i]);
}
if(br!= null){
br.close();
br = null;
}
return arr;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询