读取外部文本文件并存储到数组中

时间:2021-11-06 15:44:35

How do i read an external text file, possibly using a Scanner and store 2 values from each line into an array.

如何读取外部文本文件,可能使用扫描仪并将每行中的2个值存储到数组中。

I would want to store the node id which is the first value of each line and the parent number which is the last value in each line.

我想存储节点id,它是每行的第一个值,父节点号是每行的最后一个值。

The text file contains what you see below

文本文件包含您在下面看到的内容

1       2,7,|0|BLACK|NULL
10      3,4,12,|3|BLACK|3
11      4,12,|4|BLACK|4
12      8,10,11,|3|BLACK|8
2       1,3,6,8,|1|BLACK|1
3       2,4,5,6,8,10,|2|BLACK|2
4       3,5,9,10,11,|3|BLACK|3
5       3,4,8,|3|BLACK|3
6       2,3,|2|BLACK|2
7       1,8,|1|BLACK|1
8       2,3,5,7,9,12,|2|BLACK|2
9       4,8,|3|BLACK|8

2 个解决方案

#1


0  

Shonna, you can achieve what you want using a HashMap, a Scanner, and some simple string parsing. Here's my whole class:

Shonna,你可以使用HashMap,Scanner和一些简单的字符串解析来实现你想要的。这是我的全班:

import java.io.*;
import java.util.*;

public class nodes {

    private static HashMap<Integer, String> map = new HashMap<Integer, String>();

    public static void main(String[] args) {
        File file = new File("nodes.txt");
        Scanner scnr = null;
        try {
            scnr = new Scanner(file);
        } catch (FileNotFoundException e) {

        }
        while(scnr.hasNext()) {
            String line = scnr.nextLine();
            String[] getId = line.split("\\s+");
            int id = Integer.parseInt(getId[0]);
            int count = 0;
            int copy = 0;
            for(int i = 0; i < line.length(); i++) {
                if(line.charAt(i) == '|')
                    count++;
                if(count == 3) {
                    copy = i;
                    break;
                }
            }
            String parent = line.substring(copy + 1);
            map.put(id, parent);
            System.out.println(map);
        }
    }

}

What it does is it reads through each line of the file, first extracting the node id. It then cycles through each character in the line until it counts three |'s, at which point we know the rest of the line will be the parent of the node. After this is done, it pairs the id with the parent in a HashMap.

它的作用是读取文件的每一行,首先提取节点ID。然后它循环遍历行中的每个字符,直到它计算三个| s,此时我们知道该行的其余部分将是节点的父节点。完成此操作后,它将id与HashMap中的父对配对。

#2


2  

A regex approach (have left the array bit as an exercise for the reader):

正则表达式方法(将数组位作为读者的练习):

BufferedReader br = new BufferedReader(new FileReader("hadoop_data.txt"));

String currentLine;
while ((currentLine = br.readLine()) != null) {

    Matcher matcher = Pattern.compile("(\\d+).*\\|(\\w+)").matcher(currentLine);
    if (matcher.matches()) {
        System.out.println(matcher.group(1) + "\t" + matcher.group(2));
            // add to array
    }
}

#1


0  

Shonna, you can achieve what you want using a HashMap, a Scanner, and some simple string parsing. Here's my whole class:

Shonna,你可以使用HashMap,Scanner和一些简单的字符串解析来实现你想要的。这是我的全班:

import java.io.*;
import java.util.*;

public class nodes {

    private static HashMap<Integer, String> map = new HashMap<Integer, String>();

    public static void main(String[] args) {
        File file = new File("nodes.txt");
        Scanner scnr = null;
        try {
            scnr = new Scanner(file);
        } catch (FileNotFoundException e) {

        }
        while(scnr.hasNext()) {
            String line = scnr.nextLine();
            String[] getId = line.split("\\s+");
            int id = Integer.parseInt(getId[0]);
            int count = 0;
            int copy = 0;
            for(int i = 0; i < line.length(); i++) {
                if(line.charAt(i) == '|')
                    count++;
                if(count == 3) {
                    copy = i;
                    break;
                }
            }
            String parent = line.substring(copy + 1);
            map.put(id, parent);
            System.out.println(map);
        }
    }

}

What it does is it reads through each line of the file, first extracting the node id. It then cycles through each character in the line until it counts three |'s, at which point we know the rest of the line will be the parent of the node. After this is done, it pairs the id with the parent in a HashMap.

它的作用是读取文件的每一行,首先提取节点ID。然后它循环遍历行中的每个字符,直到它计算三个| s,此时我们知道该行的其余部分将是节点的父节点。完成此操作后,它将id与HashMap中的父对配对。

#2


2  

A regex approach (have left the array bit as an exercise for the reader):

正则表达式方法(将数组位作为读者的练习):

BufferedReader br = new BufferedReader(new FileReader("hadoop_data.txt"));

String currentLine;
while ((currentLine = br.readLine()) != null) {

    Matcher matcher = Pattern.compile("(\\d+).*\\|(\\w+)").matcher(currentLine);
    if (matcher.matches()) {
        System.out.println(matcher.group(1) + "\t" + matcher.group(2));
            // add to array
    }
}