java如何实现数值型二维数组和txt文件间的读存

justmewei 2010-03-08 04:37:13
现有一数值型二维数组 double[][] value = new double[][] {
{ 5, 9, 2 },
{ 3, -4, 5 },
{ 2, 5, 1 },
{ -8, 3, 3 }};
现欲将其存入本地文件d:\\1.txt中,存入后再将数据读入 double[][] value,如何实现?
...全文
163 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
justmewei 2010-03-09
  • 打赏
  • 举报
回复
引用 2 楼 goldenfish1919 的回复:
Java codeimport java.io.*;publicclass Test{privatestaticdouble[][] value=newdouble[][] {
{5,9,2 },
{3,-4,5 },
?-


程序确实写的不错,学习学习,谢谢了。 不过为什么在将数组内容写入d:\\1.txt中后,显示的是乱码: ur [[D黔dgE xp ur [D>玞Z xp @ @" @ uq ~ @ @ @ uq ~ @ @ ?? uq ~ @ @ @
liguang168 2010-03-09
  • 打赏
  • 举报
回复
既然有人回复了。我就顶一下OK了。
stu202060510 2010-03-09
  • 打赏
  • 举报
回复
楼上的写得很不错,学习了.......
若鱼1919 2010-03-08
  • 打赏
  • 举报
回复

import java.io.*;
public class Test{
private static double[][] value = new double[][] {
{ 5, 9, 2 },
{ 3, -4, 5 },
{ 2, 5, 1 },
{ -8, 3, 3 }
};
public static void write(String filepath)throws Exception{
OutputStream out=new FileOutputStream(filepath);
ObjectOutputStream oout=new ObjectOutputStream(out);
oout.writeObject(value);
}
public static double[][] read(String filepath)throws Exception{
InputStream in=new FileInputStream(filepath);
ObjectInputStream oin=new ObjectInputStream(in);
return (double[][])(oin.readObject());
}
public static void print(double[][] arr){
if(arr==null){
System.out.println("数组为空!");
return;
}
int row=arr.length;
int col=arr[0].length;
//System.out.println("row="+row+",col="+col);
System.out.println("{");
for(int i=0;i<row;i++){
System.out.print("{");
for(int j=0;j<col;j++){
System.out.print(arr[i][j]+" ");
}
System.out.print("}");
System.out.println();
}
System.out.println("}");
}
public static void main(String args[])throws Exception{
String filepath="d:\\1.txt";
write(filepath);
double[][] readResult=read(filepath);
print(readResult);

}
}
stu202060510 2010-03-08
  • 打赏
  • 举报
回复
我自己写了一个,你参考下:
public class Test1 {
public static void main(String[] args) throws IOException {
double[][] value = new double[][] { { 5, 9, 2 }, { 3, -4, 5 },
{ 2, 5, 1 }, { -8, 3, 3,3 } };
File f = new File("d:/1.txt");
writeArray(value,f);

ArrayList<ArrayList<Double>> readArray = readArray(f);
System.out.println(readArray);
}

public static ArrayList<ArrayList<Double>> readArray(File f){
ArrayList<ArrayList<Double>> arrList=new ArrayList<ArrayList<Double>>();
try {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String line="";
while((line = br.readLine())!=null){
String[] split = line.split(",");
ArrayList<Double> list=new ArrayList<Double>();
for(String str:split){
list.add(Double.parseDouble(str));
}
arrList.add(list);
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return arrList;
}

public static void writeArray(double[][] arr,File file){
try {
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
for (double[] v : arr) {
for (double d : v) {
bw.write(String.valueOf(d)+",");
}
bw.newLine();
}
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

如果你实在想返回二维数组的话,就自己写个转换的函数

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧