JAVA读取矩阵文件,矩阵数据的文件格式为.bak,或grd,矩阵数据交换
package com.gsafety.weixin.utils;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class MatrixUtils {
public static void main(String[] args) {
String[][] arrs =new String[120][120];
String[][] retArr=new String[120][120];
try {
File file =new File("E:\\test.bak");
InputStreamReader is=new InputStreamReader(new FileInputStream(file));
BufferedReader br =new BufferedReader(is);
String line="";
int n=0;
while((line=br.readLine())!=null){
String [] retline=line.split("\\s+");
arrs[n] = retline;
n++;
}
//实现矩阵上下对折
for (int i = 0; i < retArr.length; i++) {
for (int j = 0; j < retArr[i].length; j++) {
retArr[arrs.length-i-1][j]=arrs[i][j];
}
}
File wfile =new File("E:\\test2.bak");
OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(wfile));
BufferedWriter bw =new BufferedWriter(os);
for (int m = 0; m < retArr.length; m++) {
StringBuilder sb=new StringBuilder();
for (String str : retArr[m]) {
sb.append(str+" ");
}
bw.write(sb.toString());
bw.newLine();
}
bw.flush();
bw.close();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}