浅谈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


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




