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

How to read these data from a file in JAVA

我有这种格式的数据

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

快速 google 搜索让我找到了这个 tutorial on basic java I/O, and in particular, there is a section on Scanning,它演示了如何方便地将您的输入分解(又名 "split" 或 "tokenize")成更小的部分并可能解析他们以更有条理的方式。

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+)\)");

我还没有测试过,但基本上“\\(”用于解析“(”,“\\w+”用于解析一个或多个字符的任意序列。如果你想解析一个数字序列然后使用 "\\d+" 代替。

希望对您有所帮助,祝您好运!

在我看来,这里最适合您的是使用属性 class。 使用属性 class 您可以设置一些属性,然后再次读取它们! 这是我发现最能解释它的 link!

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

像这样使用匹配器class

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