文件的打开并输出
如D盘一文件text.txt,内容为:
12345
2321
2.23
2.3
343.233443132081111122222
2321
2.23
2.3
343.233443132081111122222
用java编程实现打开并输出:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class file {
public static void main(String[] args) {
try{
File file = new File("D:\\Text.txt");//创建文件对象
BufferedReader br = new BufferedReader(new FileReader(file)); //创建读取流
//读取数据
String temp = br.readLine();
String line;
while((line = br.readLine()) != null){
temp += "," + line;
}
if(temp == null){
System.out.println();
}else{
//分割字符串
String []str = temp.split(",");
//创建double数组并赋值
Double[] d = new Double[str.length];
for(int i = 0 ;i < str.length ;i++){
d[i] = Double.parseDouble(str[i]);
}
//打印double数组
for(int i = 0 ; i < d.length ; i++){
System.out.println(d[i]);
}
}
}catch(Exception e){
}
}
}
输出结果:
12345.0
2321.0
2.23
2.3
343.2334431320811
2321.0
2.23
2.3
343.2334431320811