62,612
社区成员
发帖
与我相关
我的任务
分享
public class Matrix{
//从文件中读取数据,将读取到的矩阵数据加载到字符串中
public String toString(String s1,String s2, String s3){
try
{ BufferedReader buffer = new BufferedReader(new FileReader(s3));
while((s1=buffer.readLine())!=null){
s2+=s1+"\n";
}
buffer.close();
//System.out.println(s2);
}catch(FileNotFoundException e){
System.out.println("未找到文件");
}catch(IOException e){
System.out.println("运行出错");
}
return s2;
}
//将得到的字符串转化为数组
public String[][] ToMatrix(String str){
String[] split = str.split("\n");
String[][] ar = new String[split.length][];
for(int i=0;i < split.length; i++){
ar[i] = split[i].split(" ");
}
return ar;
}
//将得到的array数组打印出来
public void Print(String[][] array){
System.out.println(array.length);
for(int i=0; i < array.length; i++){
for(int j=0;j<array[i].length;j++){
System.out.println(array[i][j]+" ");
System.out.println("i"+i+"j:"+j);
}
System.out.println();
System.out.println("打印成功!");
}
}
}
public class TestMatrix {
public static String str1 = "";
public static String str2 = "";
public static String str3 = "E:/Java learning materials/test.txt";
public static void main(String[] args) {
Matrix matrix =new Matrix();
matrix.toString(str1, str2, str3);
String[][] array = matrix.ToMatrix(str2);
matrix.Print(array);
System.out.println(str2);
}
}