Java实现文件的读取并初始化给数组
请教高手!如何实现java读取一个文件
并把文件的内容以字符形式逐个传给
一个数组.
如一个map.txt文件内容为:
##I############
## ##### ###
### #### #####
### ## ###
### ## #####
######### #
#############O#
把map.txt读取传给
数组 char[][]
然后可通过数组打印出来
而且map.txt文件里面的字符数是变化的
数组char[][]也是变换的不固定的
希望高手帮忙指点 我想用java实现一个迷宫问题.
我没有积分了只能给这么多了,呵呵.
------解决方案--------------------import java.io.*;
class iomap {
public static void main(String[] args) {
char[][] c =null;
int count1 = 0;
int count2 =0;
BufferedReader input = null;
try{
input = new BufferedReader(new FileReader( "d:/map.txt "));
String buf;
int max =0 ;
while ((buf = input.readLine()) != null){
count1++;
count2 = buf.length();
if (max <count2){
max=count2;
}
}
}catch (
FileNotFoundException e) {
System.out.println( "Can 't find the file!!! ");
} catch (
IOException e) {
e.printStackTrace();
} finally {
if (input != null)
try {
input.close();
} catch (IOException e) {
}
}
c = new char[count1][count2];
try {
input = new BufferedReader(new FileReader( "d:/map.txt "));
String buf;
int count3 =0;
while ((buf = input.readLine()) != null) {
for (int i = 0; i < buf.length(); i++) {
c[count3][i]=buf.charAt(i);
}
count3++;
}
} catch (File
NotFoundException e) {
System.out.println( "Can 't find the file!!! ");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (input != null)
try {
input.close();
} catch (IOException e) {
}
}
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[i].length; j++) {
System.out.print(c[i][j]);
}
System.out.println();
}
}
}