欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

浅谈java 数据处理(int[][]存储与读取)

时间:2021-04-08 10:40:19|栏目:JAVA代码|点击:

MyFile .java:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class MyFile {

public static void SaveFile(String filename,int[][] arr){
 try {
 File file = new File(filename); //存放数组数据的文件
 FileWriter out = new FileWriter(file); //文件写入流
 try {
 getRecord(out,arr);
 } catch (Exception e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } 
 out.close(); 
} catch (Exception ex) {
ex.printStackTrace();
}
} 
private static void getRecord(FileWriter out,int[][] arr)
throws Exception {
 //将数组中的数据写入到文件中。每行各数据之间TAB间隔
 for(int i=0;i<arr.length;i++){
 for(int j=0;j<arr[0].length;j++){
  out.write(arr[i][j]+"\t");
 }
 out.write("\r\n");
 } 
} 

public static void ReadFile(String filename,int[][] arr2){
 try {
File file = new File(filename); //存放数组数据的文件
 BufferedReader in = new BufferedReader(new FileReader(file)); //
 
 try {
 readRecord(in,arr2);
 } catch (Exception e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } 
 in.close(); 
} catch (Exception ex) {
ex.printStackTrace();
}
}

private static void readRecord(BufferedReader in,int[][] arr2)
throws Exception {
String line; //一行数据
 int row=0;
 //逐行读取,并将每个数组放入到数组中
 while((line = in.readLine()) != null){
  String[] temp = line.split("\t"); 
  for(int j=0;j<temp.length;j++){
//   arr2[row][j] = Double.parseDouble(temp[j]);
  arr2[row][j] = Integer.parseInt(temp[j]);
  }
  row++;
 }
} 
}

使用:

public static int imagedate[ ][ ];
MyFile.SaveFile("d:\\array.txt",imagedate);

上一篇:Java实现发红包功能

栏    目:JAVA代码

下一篇:SpringCloud Gateway 利用 Mysql 实现动态路由的方法

本文标题:浅谈java 数据处理(int[][]存储与读取)

本文地址:http://www.codeinn.net/misctech/97248.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有