我如何从文本文件中获取字符并将其放入2D数组?

时间:2022-09-28 14:29:57

for example:

-8-88-8-8-8--8

-8-8-8-8-8-8-8

*-8-8-8-8--8-8

8--8-8-8-8--8-

3 个解决方案

#1


2  

  1. Read text file line by line
  2. 逐行读取文本文件

  3. Split string by - so you will have char array add it in your 2d char array.
  4. 拆分字符串 - 所以你将char数组添加到你的2d char数组中。

#2


2  

The easiest way would be to use the toCharArray() method of String.

最简单的方法是使用String的toCharArray()方法。

So if you are using a BufferedReader

因此,如果您使用的是BufferedReader

   ArrayList<String> list = new ArrayList<String>();

   BufferedReader br = new BufferedReader(new FileReader("file"));
   while ((thisLine = br.readLine()) != null) { 
     list.add(thisLine);
   } 

   // finally convert the arraylist to a char[]
   char[] firstDimension = new char[list.size()];
   for (int i = 0; i < list.length; i++) {
       firstDimension[i] = list.get(i).toCharArray();
   }

#3


0  

char[] firsDimension is not 2d. Try something like,

char [] firsDimension不是2d。试试像,

    try {
        List<String> list = new ArrayList<String>();

        String thisLine = null;
        BufferedReader br;
        br = new BufferedReader(new FileReader("/path/to/file/yourfile.txt"));

        while ((thisLine = br.readLine()) != null) {
            list.add(thisLine);
        }

        char[][] firstDimension = new char[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            firstDimension[i] = list.get(i).toCharArray();
        }

        //print values
        for (int i=0;i<firstDimension.length;i++) {
            for (int j=0;j<firstDimension[i].length;j++) {
                System.out.println(firstDimension[i][j]);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

With scanner class

使用扫描仪类

    try {
        Scanner scanner = new Scanner(new File(
                "/home/sinan/Desktop/yourfile.txt"));
        scanner.useDelimiter(System.getProperty("line.separator"));

        ArrayList<String> list = new ArrayList<String>();

        while (scanner.hasNext()) {
            list.add(scanner.next());
        }
        scanner.close();

        // finally convert the arraylist to a char[][]
        char[][] firstDimension = new char[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            firstDimension[i] = list.get(i).toCharArray();
        }

        for (int i = 0; i < firstDimension.length; i++) {
            for (int j = 0; j < firstDimension[i].length; j++) {
                System.out.println(firstDimension[i][j]);
            }
        }

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

#1


2  

  1. Read text file line by line
  2. 逐行读取文本文件

  3. Split string by - so you will have char array add it in your 2d char array.
  4. 拆分字符串 - 所以你将char数组添加到你的2d char数组中。

#2


2  

The easiest way would be to use the toCharArray() method of String.

最简单的方法是使用String的toCharArray()方法。

So if you are using a BufferedReader

因此,如果您使用的是BufferedReader

   ArrayList<String> list = new ArrayList<String>();

   BufferedReader br = new BufferedReader(new FileReader("file"));
   while ((thisLine = br.readLine()) != null) { 
     list.add(thisLine);
   } 

   // finally convert the arraylist to a char[]
   char[] firstDimension = new char[list.size()];
   for (int i = 0; i < list.length; i++) {
       firstDimension[i] = list.get(i).toCharArray();
   }

#3


0  

char[] firsDimension is not 2d. Try something like,

char [] firsDimension不是2d。试试像,

    try {
        List<String> list = new ArrayList<String>();

        String thisLine = null;
        BufferedReader br;
        br = new BufferedReader(new FileReader("/path/to/file/yourfile.txt"));

        while ((thisLine = br.readLine()) != null) {
            list.add(thisLine);
        }

        char[][] firstDimension = new char[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            firstDimension[i] = list.get(i).toCharArray();
        }

        //print values
        for (int i=0;i<firstDimension.length;i++) {
            for (int j=0;j<firstDimension[i].length;j++) {
                System.out.println(firstDimension[i][j]);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

With scanner class

使用扫描仪类

    try {
        Scanner scanner = new Scanner(new File(
                "/home/sinan/Desktop/yourfile.txt"));
        scanner.useDelimiter(System.getProperty("line.separator"));

        ArrayList<String> list = new ArrayList<String>();

        while (scanner.hasNext()) {
            list.add(scanner.next());
        }
        scanner.close();

        // finally convert the arraylist to a char[][]
        char[][] firstDimension = new char[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            firstDimension[i] = list.get(i).toCharArray();
        }

        for (int i = 0; i < firstDimension.length; i++) {
            for (int j = 0; j < firstDimension[i].length; j++) {
                System.out.println(firstDimension[i][j]);
            }
        }

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