package com.yp.algorithm;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Readfile2 {
public static void main(String[] args) {
FileReader file = null;
try {
file = new FileReader(“.//src//test.dat”);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println(“文件测试数据如下:”);
txtString(file);
}
public static void txtString(FileReader file){
BufferedReader br = new BufferedReader(file);//读取文件
try {
String line = br.readLine();//读取一行数据
int lines = Integer.parseInt(line);//将数据转化为int类型
System.out.println(lines);
String \[\]sp = null;
String \[\]\[\]c = new String\[lines\]\[lines\];
int \[\]\[\]cc = new int\[lines\]\[lines\];
int count=0;
while((line=br.readLine())!=null) {//按行读取
sp = line.split(" ");//按空格进行分割
for(int i=0;i<sp.length;i++){
c\[count\]\[i\] = sp\[i\];
}
count++;
}
for(int i=0;i<lines;i++){
for(int j=0;j<lines;j++){
cc\[i\]\[j\] = Integer.parseInt(c\[i\]\[j\]);
System.out.print(cc\[i\]\[j\]);
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}