简体   繁体   English

尝试从文本文件中获取信息并将信息拆分为数组

[英]Trying to get info from a textfile and split the info in an array

I'm having a problem with splitting a textfile and getting the info from the textfile to an array so that i can choose the index that I want to print. 我有分裂文本文件和从文本文件获取信息到数组的问题,以便我可以选择我想要打印的索引。 The textfile is formatted like this: 文本文件的格式如下:

X;25
Y;15

What I want to do is split the file so I can print X 25 and Y 15 or if I can get it to print just everything separately: X, 25, Y, 15 我想要做的是分割文件,这样我就可以打印X 25和Y 15,或者我是否可以单独打印所有内容: X, 25, Y, 15

This is my code, I've tried very many different variations but right now I'm completely stuck. 这是我的代码,我尝试了很多不同的变体,但现在我完全陷入了困境。 I've been tipped about HashMap but have no idea how to do that. 我已经向人们介绍过HashMap但不知道该怎么做。 Anybody who can fix my problem? 谁能解决我的问题?

BufferedReader BR = new BufferedReader(new FileReader(new File(textfile)));

StringBuilder SB = new StringBuilder();
String assign = BR.readLine();

while((assign = BR.readLine()) != null) {
    if (assign.contains(";")) {
        SB.append(assign);   
        String assignment = SB.toString();
        String[] splitting = assignment.split(";");

        for (String s : splitting) {
            System.out.println(s);
        }  
    }
}

Why the whole StringBuilder complication? 为什么整个StringBuilder复杂化? (not tested) (未测试)

BufferedReader BR = new BufferedReader(new FileReader(new File(textfile)));

        String assign = BR.readLine(); //skips first line (if you have a header)

        while((assign = BR.readLine()) != null){

            String[] splitting= assign.split(";");

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

        }

This should work: 这应该工作:

BufferedReader BR = new BufferedReader(new FileReader(new File(textfile)));

String assign;

while((assign = BR.readLine()) != null){

    String[] splitting= assign.split(";");

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

}

If you like to retrieve the content of the file (and not just print it), then you can try the code below. 如果您想检索文件的内容(而不仅仅是打印它),那么您可以尝试下面的代码。 There I use a Map to store the file content and print it later. 在那里,我使用Map来存储文件内容并在以后打印。

private static final String DELIMITER = ";"; // define a constant for the used delimiter

public static void main(String args[]) throws Exception {
    final Map<String, Integer> fileContent = readFile("test.txt");
    System.out.println(fileContent);
}

public static Map<String, Integer> readFile(final String fileName) {
    final Map<String, Integer> content = new HashMap<>(); // define a map for the file content
    // try-with-resources statement so the file will be closed safely
    try (final FileReader fileReader = new FileReader(new File(fileName));
         final BufferedReader bufferedReader = new BufferedReader(fileReader)) {
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            if (line.contains(DELIMITER)) {
                final String[] parts = line.split(DELIMITER);
                // the second value is an integer, so we need to convert it
                content.put(parts[0], Integer.parseInt(parts[1]));
            }
        }
    } catch (IOException e) {
        // handle exception a bit more, if you like ...
        e.printStackTrace();
    }
    return content;
}

The output will be: 输出将是:

{X=25, Y=15}

That means the map fileContent contains the keys "X" and "Y" and their values are 25 and 15 . 这意味着map fileContent包含键"X""Y" ,它们的值为2515 If you like to get the value for "X" , then use int valueX = fileContent.get("X"); 如果你想得到"X"的值,那么使用int valueX = fileContent.get("X"); .

Your current code won't close the opened file reader, that is why I've wrapped my code in a try-with-resources statement . 您当前的代码不会关闭打开的文件阅读器,这就是我将代码包装在try-with-resources语句中的原因 It will handle that for me. 它会为我处理。

If there occurs an IOException (file not found, or file couldn't be read, or something like this), then I currently just print the stacktrace. 如果发生IOException (找不到文件,或无法读取文件,或类似的东西),那么我目前仅打印stacktrace。 You can handle this case differently, if you like: tutorial . 如果愿意,您可以以不同的方式处理这种情况: tutorial

Try to play with the above code to see how it works. 尝试使用上面的代码来看看它是如何工作的。 I hope it helps you. 我希望它对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM