目录
- 1.将二维数组存入txt文件的步骤
- 2.读取txt文件转换为二维数组
- 3.测试用例
1.将二维数组存入txt文件的步骤
public static void saveArray(int[][] array){
//1.创建字符输出流
FileWriter writeFile = null;
try {
//2.数据想写入的路径及文件
File file = new File("Array.txt");
//3.如果该文件不存在,就创建
if(!file.exists()) {
file.createNewFile();
}
//4.给字节输出流赋予实例
writeFile = new FileWriter(file);
//5.通过循环将数组写入txt文件中
for(int i = 0; i < array.length; i++) {
//6.数据前n - 1列尾部加入","
for(int j = 0; j < array[0].length - 1; j++) {
writeFile.write(array[i][j] + ",");
}
//7.数组最后一列后面不加","
writeFile.write(array[i][array[0].length - 1] + "");
//8.加上换行符
writeFile.write("\n");
}
//9.把writeFile里的数据全部刷新一次,全部写入文件中
writeFile.flush();
} catch (Exception e) {//10.异常捕获
e.printStackTrace();
} finally {
try {
//11.如果writeFile不为空,就将其关闭
if(writeFile != null)
writeFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.读取txt文件转换为二维数组
public static int[][] readArray() {
//1.声明一个字符输入流
FileReader reader = null;
//2.声明一个字符输入缓冲流
BufferedReader readerBuf = null;
//3.声明一个二维数组
int[][] array = null;
try {
//4.指定reader的读取路径
reader = new FileReader("Array.txt");
//5.通过BufferedReader包装字符输入流
readerBuf = new BufferedReader(reader);
//6.创建一个集合,用来存放读取的文件的数据
List<String> strList = new ArrayList<>();
//7.用来存放一行的数据
String lineStr;
//8.逐行读取txt文件中的内容
while((lineStr = readerBuf.readLine()) != null) {
//9.把读取的行添加到list中
strList.add(lineStr);
}
//10.获取文件有多少行
int lineNum = strList.size();
//11.获取数组有多少列
String s = strList.get(0);
int columnNum = s.split("\\,").length;
//12.根据文件行数创建对应的数组
array = new int[strList.size()][columnNum];
//13.记录输出当前行
int count = 0;
//14.循环遍历集合,将集合中的数据放入数组中
for(String str : strList) {
//15.将读取的str按照","分割,用字符串数组来接收
String[] strs = str.split("\\,");
for(int i = 0; i < columnNum; i++) {
array[count][i] = Integer.valueOf(strs[i]);
}
//16.将行数 + 1
count++;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//17.关闭字符输入流
try {
if(reader != null)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
//18.关闭字符输入缓冲流
try {
if(readerBuf != null)
readerBuf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//19.返回稀疏数组
return array;
}
3.测试用例
把下面这段代码和上面两段代码都复制到一个class文件中,保存运行一下,就能看到运行结果啦~~
public static void main(String args[]) {
int[][] arr = new int[5][5];
for(int i = 0; i < arr.length; i++) {
for(int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
saveArray(arr);
System.out.println();
int[][] arr1 = readArray();
for(int i = 0; i < arr1.length; i++) {
for(int j = 0; j < arr1.length; j++) {
System.out.print(arr1[i][j] + " ");
}
System.out.println();
}
}