TXT文本中如据形如:
123
456
789读入二维数组效果为:
temp[0][]={1,2,3};
temp[1][]={4,5,6};
temp[2][]={7,8,9};新手
请大家指点

解决方案 »

  1.   

    一次读一个字节。然后将这个字节赋值给数组中的一个元素。                int temp[][] = new int[100][100];

    FileInputStream fis = new FileInputStream("c:\\1.txt");
    int input;
    while(true){

    input = fis.read();
    if(input == -1){
    break;
    }
    //将input赋值给数组中元素。

    }
      

  2.   

    1楼的不全对,应该用String input = fis.readLine(); 读取一行,然后将这行分解存入二维数组中.
      

  3.   

    1楼:read() 
              从此输入流中读取下一个数据字节。
    一个数字是四字节,好像用read()不好实现
      

  4.   


    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.*;
    public class xx{
    public static void main(String[]args){
    String s;
    int[][]save=new int[3][3];
    try{
    BufferedReader in =new BufferedReader(new FileReader("C:\\txt.txt"));
    int i=0;
    while((s=in.readLine())!=null){
    save[i][0]=Integer.parseInt(s.substring(0,1));
    save[i][1]=Integer.parseInt(s.substring(1,2));
    save[i][2]=Integer.parseInt(s.substring(2,3));
    i++;
    }
    }
    catch(FileNotFoundException e){
    e.printStackTrace();
    }
    catch(IOException e){
    e.printStackTrace();
    }
    for(int i=0;i<3;i++)
    {
    for(int j=0;j<3;j++){
    System.out.print(save[i][j]);
    }
    System.out.println();
    }
    }
    }
      

  5.   

    用BufferedReader读取一行,然后分割,转成int类型后存入数组
      

  6.   

    超简单的问题BufferedReader bf=new BufferedReader(new FileReader("Your file"));
    String lineContent=null;
    int i = 0;
    int [][] temp = new int [3][];
    while((lineContent=bf.readLine())!=null){
    String [] str = lineContent.split("\\d");// 将 lineContent 按数字拆分
    for(int j = 0; j < str.length(); j++){
    int [i][j] = Integer.parseInt(str[j]);
    }
    i++;
    }
      

  7.   

    可以先读一行InLine(),读出的是string类型的,然后利用stringToken 把字符分析出来 放入数组
      

  8.   

    public static void getStringData(String path){
    try {
    String str="";
    BufferedReader br=new BufferedReader(new FileReader(path));
    int len;
    int k=0;
    char[][] data=new char[100][100];
    char[] temp=new char[100];
    while((len=br.read(temp))!=-1){
    for(int i=0;i<100;i++){
    data[k][i]=temp[i];
    }
    k++;
    }
    for(int i=0;i<k;i++){
    for(int j=0;j<100;j++){
    System.out.print(data[i][j]);
    if(j==99){
    System.out.println();
    }
    }
    }

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
      

  9.   

    public static void getStringData(String path){
    try {
    String str="";
    BufferedReader br=new BufferedReader(new FileReader(path));
    int len;
    int k=0;
    char[][] data=new char[100][100];
    char[] temp=new char[100];
    while((len=br.read(temp))!=-1){
    for(int i=0;i<100;i++){
    data[k][i]=temp[i];
    }
    k++;
    }
    for(int i=0;i<k;i++){
    for(int j=0;j<100;j++){
    System.out.print(data[i][j]);
    if(j==99){
    System.out.println();
    }
    }
    }

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
      

  10.   

    public static void getStringData(String path){
    try {
    String str="";
    BufferedReader br=new BufferedReader(new FileReader(path));
    int len;
    int k=0;
    char[][] data=new char[100][100];
    char[] temp=new char[100];
    while((len=br.read(temp))!=-1){
    for(int i=0;i<100;i++){
    data[k][i]=temp[i];
    }
    k++;
    }
    for(int i=0;i<k;i++){
    for(int j=0;j<100;j++){
    System.out.print(data[i][j]);
    if(j==99){
    System.out.println();
    }
    }
    }

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
      

  11.   

    package org.eros.sharon;import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;import java.util.List;public class ArrayConvert1 { public static int[][] getArray(File file) {
    try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String str = null;
    List<int[]> list = new ArrayList<int[]>();
    while ((str = br.readLine()) != null) {
    if (str.length() > 0) {// 去掉空行
    int[] temp = toArray(str.toCharArray());
    list.add(temp);
    }
    }
    return list.toArray(new int[][] {});
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return null;
    } public static int[][] getArray(String path) {
    return getArray(new File(path));
    } private static int[] toArray(char[] args) {
    int[] temp = new int[args.length];
    for (int i = 0; i < args.length; i++) {
    temp[i] = args[i] - '0';
    }
    return temp;
    } private static int[] toArray(String[] args) {
    int[] temp = new int[args.length];
    for (int i = 0; i < args.length; i++) {
    temp[i] = Integer.valueOf(args[i]);
    }
    return temp;
    } /**
     * 
     * @param path
     *            文件路径
     * @param regex
     *            分割符 ArrayConvert 按照指定的分割符进行数组转换
     * @return
     */
    public static int[][] getArray(File file, String regex) {
    try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String str = null;
    List<int[]> list = new ArrayList<int[]>();
    while ((str = br.readLine()) != null) {
    if (str.length() > 0) {// 去掉空行
    int[] temp = toArray(str.split(regex));
    list.add(temp);
    }
    }
    return list.toArray(new int[][] {});
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return null;
    }

    public static int[][] getArray(String path,String regex){
    return getArray(path,regex);
    }}
      

  12.   

    测试类
    package org.eros.sharon;import java.io.File;import org.junit.Test;public class TestArray { @Test
    public void test() {
    int[][] temp = ArrayConvert.getArray("d:/1.txt");
    for (int i = 0; i < temp.length; i++) {
    for (int j = 0; j < temp[i].length; j++) {
    System.out.print(temp[i][j] + " ");
    }
    System.out.println();
    }

    } @Test
    public void test1() {
    String str = ";";
    int[][] temp = ArrayConvert1.getArray(new File("d:/2.txt"), str);
    for (int i = 0; i < temp.length; i++) {
    for (int j = 0; j < temp[i].length; j++) {
    System.out.print(temp[i][j] + " ");
    }
    System.out.println();
    }

    }}1.txt
    123
    456
    7892.txt
    121;343;3;
    232;3;23;
    23;4;533;
      

  13.   

    楼主参阅http://blog.csdn.net/moshangchenzi/archive/2008/11/11/3270483.aspx输入输出,对象流都写上了
      

  14.   

    split("\\d") 正则表达式应该是"\\ ",以空格为分割符