代码之家  ›  专栏  ›  技术社区  ›  Ahmad tamer

如何从JAVA文件中读取这些数据

  •  3
  • Ahmad tamer  · 技术社区  · 8 年前

    我有这种格式的数据

    n
    l
    p1(x1) p1(x2) imp(p1)
    p2(x1) p2(x2) imp(p2)
    : : :
    pl(x1) pl(x2) imp(pl)
    r
    q1(x1) q1(x2) imp(q1)
    q2(x1) q2(x2) imp(q2)
    : : :
    qr(x1) qr(x2) imp(qr)
    

    其中,n、l(p的数量)和r(q的数量)是整数。我知道如何从文件中只读整数,但如何读取包含字符串的行,并获得每行的x1、x2和p1的值?!提前谢谢。

    这是我只读取整数的代码。

    try {
                reader = new BufferedReader(new FileReader(file));
                String text = null;
                while ((text = reader.readLine()) != null) {
                    // The first line gives the number of nodes (You will use to create the int[][] graph = new int[nOfNodes][nOfNodes];)
                    if (c == 0) {
                        numberOfNodes = Integer.parseInt(text.trim());
                    } // The second one gives the number of edges
                    else if (c == 1) {
                        nOfEdges = Integer.parseInt(text.trim());
                        graph2 = new double[nOfEdges][3];
                    } // And third the list of special nodes
                    // `nodes` will now contains only your special constrained one
                    else if (c == 2) {
                        String[] str = text.split(" ");
                        for (int i = 0; i < str.length; i++) {
                            if (str[i].trim().length() > 0) {
                                nodes.add(Integer.parseInt(str[i]));
                            }
                        }
                    } else { // Then you have your edges descriptors
                        String[] str = text.split(" ");
                        for (int i = 0; i < str.length; i++) {
                            if (str[i].trim().length() > 0) {
                                graph2[c - 4][i] = Double.parseDouble(str[i]);
                            }
                        }
                    }
                    c++;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    System.out.print(e);
                }
            }
    
    3 回复  |  直到 8 年前
        1
  •  0
  •   Subhi    8 年前

    像这样使用matcher类

    public static int numberOfPoints = -1, p=-1, q=-1;
    
        public static void main(String[] args) throws FileNotFoundException {
    
            ArrayList<Integer> dots = new ArrayList<>();
            int c = 0;
            File file = new File("D:\\ALGORTHIMS\\MASTER LEVEL\\dr. khaled\\assignment 3\\a.txt");
            BufferedReader reader = null;
    
    
            try {
                reader = new BufferedReader(new FileReader(file));
                String text = null;
                while ((text = reader.readLine()) != null) {
    
                    try {
                        if(c==0)
                        { numberOfPoints=Integer.parseInt(text); c=1;}
                        if(c==1)
                        { p=Integer.parseInt(text);c=2;}
                        if(c==2)
                            q=Integer.parseInt(text);
    
                    } catch (NumberFormatException e) {
    
                        Matcher m = Pattern.compile("\\(([^)]+)\\)").matcher(text);
            while (m.find()) {
    
               dots.add(Integer.parseInt(m.group(1)));
            }
    
                    }
    
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    System.out.print(e);
                }
            }
    
        2
  •  0
  •   Joza100    8 年前

    在我看来,这里对您最好的是使用Properties类。 使用properties类,您可以设置一些属性,然后再次读取它们! 这是我找到的最能解释它的链接!

    https://www.mkyong.com/java/java-properties-file-examples/

    如果您不想这样做,可以简单地将行拆分为3个字符串,每个字符串包含一段数据。

    String data[] = line.split(" ");
    

    现在找到开放括号和封闭括号,并得到它们之间的内容。 我添加了+1和-1,这样它就不会同时读取大括号,只读取介于两者之间的数字。

    int begin = string.indexOf('(');
    int end = string.indexOf(')');
    string.substring(begin + 1, end - 1);
    

    现在只需将字符串解析为整数。

    int number = Integer.parseInt(thedatathatyouhave);
    
        3
  •  0
  •   Parakram Majumdar    8 年前

    快速的谷歌搜索让我找到了这个 tutorial on basic java I/O ,尤其是 section on Scanning 这演示了如何方便地将输入分解成更小的部分,并可能以更结构化的方式解析它们。

    这个 documentation for the Scanner class 有更详细的示例。我想你需要这样的东西:

    String input = "p1(x1) p1(x2) imp(p1)";
    Scanner s = new Scanner(input);
    s.findInLine("(\\w+)\\((\\w+)\\) (\\w+)\\((\\w+)\\) imp\\((\\w+)\\)");
    

    我还没有对此进行测试,但基本上“\\”(“用于解析a”(“),“\\w+”用于解析一个或多个字符的任何序列。如果要解析一个数字序列,请改用“\\d+”。

    希望这有帮助,祝你好运!