请教高手!如何实现java读取一个文件
并把文件的内容以字符形式逐个传给
一个数组.
如一个map.txt文件内容为:
##I############
##  #####   ###
###  #### #####
### ##      ###
###    ## #####
#########     #
#############O#
把map.txt读取传给
数组 char[][]
然后可通过数组打印出来
而且map.txt文件里面的字符数是变化的
数组char[][]也是变换的不固定的
希望高手帮忙指点 我想用java实现一个迷宫问题.
我没有积分了只能给这么多了,呵呵.

解决方案 »

  1.   

    import java.util.*;
    import java.io.*;public class PazzleMaps {
    public static void main(String[] args) throws Exception {
    Scanner sc = new Scanner(new FileReader("map.txt"));
    ArrayList<String> al = new ArrayList<String>();
    while(sc.hasNextLine()) {
    String s = sc.nextLine();
    al.add(s);
    }
    for(String ss : al) {
    System.out.println(ss);
    }
    }
    }
      

  2.   

    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 (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) {
    }
    }

    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();
    } }
    }
      

  3.   

    zephyr_cc() 的方法好好呀。
    学习