Java文本文件输入处理

时间:2014-10-13 22:27:24

标签: java inputstream bufferedreader

我正在尝试从文本文件中读取数据,我不太清楚如何处理这个问题。

例如,如果我的数据在文本文件中显示如下:

2   
3   
(0 1 1)   
(0 2 3)

我想以下列方式保存这些值:

int a = 2;
int b = 3;   
int[][] c = new int[a][b];   
c[0][1] = 1   
c[0][2] = 3

括号中的数字是(行,列,值),我应该从数据中填写二维数组。

我对输入流非常新。我需要做些什么来使我的程序识别括号和括号内的数据应该以不同的方式处理?

2 个答案:

答案 0 :(得分:0)

首先,您需要初始化像BufferedReader或Scanner这样的Util类,而不是能够管理txt文件。

这是一段java代码的示例,它将txt文件中的所有数字都作为Double类型,然后将它们保存到列表中。

您可以对整数执行相同操作,或者,只需使用所需方法(检查BufferedReader和Scanner javadocs)将每一行格式化为String,以根据需要处理它们。

Scanner s = null;

s = new Scanner(new BufferedReader(new FileReader("YOUFILENAME.txt")));
        while (s.hasNext()) {
            if (s.hasNextDouble()) {
                \\\ YOURLIST.add (s.nextDouble ())
            } else {
                s.next();
            }   
        }
    } finally {
        s.close();
    }

要管理(之类的字符,您可以使用字符串类型的内置方法,例如split ()contains ()

另请参阅:http://docs.oracle.com/javase/tutorial/essential/io/scanning.html

答案 1 :(得分:0)

有很多方法可以做到这一点,这可能不是最优雅的,但这样的方法很有效。我使用正则表达式来分割括号。

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Arrays;

public class Test {
    public static void main(String[] args) throws Exception {
      Scanner scan = new Scanner(System.in);

      // Read array dimensions
      String line = "";

      line = scan.hasNext() ? scan.nextLine() : null;
      if(line == null) {
        throw new Exception("Invalid input for 'a'");
      }

      int a = -1;
      try {
        a = Integer.valueOf(line);
      } catch(NumberFormatException e) { 
        System.out.println("Input is not a number: " + line);
        return;
      }

      int b = -1;
      line = scan.hasNext() ? scan.nextLine() : null;
      if(line == null) {
         throw new Exception("Invalid input for 'b'");
      }
      try {
        b = Integer.valueOf(line);
      } catch(NumberFormatException e) {
        System.out.println("Input is not a number: " + line);
        return;
      }

      if(a <= 0 || b <= 0) {
         throw new Exception("Array sizes should be larger than 0");
      }
      int array[][] = new int[a][b];

      // Gather array values
      while(scan.hasNext()) {
          line = scan.nextLine();
          if(line.charAt(0) == 'q') { break; } // 'q' to exit
          setArray(array, line);
      }

      // Print array
      for(int i=0; i < array.length; ++i) {
          System.out.println(Arrays.toString(array[i]));
      }
    }

    // Returns true on set success, false otherwise
    private static boolean setArray(int[][] array, String input) throws NumberFormatException {
      Pattern regex = Pattern.compile("\\((\\d*?) (\\d*?) (\\d*?)\\)");
      Matcher match = regex.matcher(input);
      if(match.matches() && match.groupCount() == 3) {
        int a = Integer.valueOf(match.group(1));
        int b = Integer.valueOf(match.group(2));
        int val = Integer.valueOf(match.group(3));

        // Check bounds
        if(a >= 0 && a < array.length && b >= 0 && b < array[0].length) {
            array[a][b] = val;
        }
        else { 
            return false;
        }
      }
      else {
        return false;
      }
      return true;
    }
}
相关问题