【问题标题】:Read a text file and store numbers in different arrays in java读取文本文件并将数字存储在java中的不同数组中
【发布时间】:2013-09-19 12:02:51
【问题描述】:

我目前正在写我的论文,在这种情况下,我需要使用 java 开发一个元启发式算法。但是,我在尝试读取和存储数据时遇到了问题。

我的文件是一个文本文件,大约有 150 行。问题的一个例子是在第 5 行,其中声明了三个整数:30、38 和 1。我想将它们中的每一个存储为分别称为 L、T 和 S 的整数,这对于许多其他的线。

你们有谁知道怎么做的吗?如果需要,我可以将 txt 文件发送给您。

顺便说一句:这是我迄今为止尝试过的:

Main.java:

import java.io.IOException;
import java.io.FileWriter;
import java.io.BufferedWriter;


public class MAIN {

public static void main(String[] args) throws IOException {
    Test.readDoc("TAP_T38L30C4F2S12_03.txt");
    }   
}

Test.java:

import java.io.*;
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Map;

public class Test {

private static ArrayList<Integer> integerList = new ArrayList<Integer>();
public static Map<String, ArrayList<Integer>> data = new HashMap<String,               ArrayList<Integer>>();
public static String aKey;

public static void readDoc(String File) {

try{
FileReader fr = new FileReader("TAP_T38L30C4F2S12_03.txt");
BufferedReader br = new BufferedReader(fr);

while(true) {
    String line = br.readLine();
    if (line == null) 
    break;
else if (line.matches("\\#\\s[a-zA-Z]")){
    String key = line.split("\\t")[1];
    line = br.readLine();
    data.put(key, computeLine(line));
    }
else if (line.matches("\\\\\\#\\s(\\|[a-zA-Z]\\|,?\\s?)+")){
     String[] keys = line.split("\\t");
     line = br.readLine();
     ArrayList<Integer> results = computeLine(line);
     for (int i=0; i<keys.length; i++){
          aKey = aKey.replace("|", "");
        //  data.put(aKey, results.get(i));
          data.put(aKey, results);
     }
    }
    System.out.println(data);

    }
} catch(Exception ex) {
    ex.printStackTrace();      }    
}


private static ArrayList<Integer> computeLine (String line){
    String[] splitted = line.split("\\t");
    for (String s : splitted) {
    integerList.add(Integer.parseInt(s));
    }

    return integerList;
}

}

数据示例如下:

     \# TAP instance 
     \# Note that the sequence of the data is important!
     \#
     \# |L|, |T|, |S|
     30 38  1
     \#
     \# v
     8213   9319    10187   12144   8206    ...
     \#
     \# w
     7027   9652    9956    13973   6661    14751   ...
     \#
     \# b
     1  1   1   1   1   ...
     \#
     \# c
     1399   1563    1303    1303    2019    ...
     \#
     \# continues

【问题讨论】:

  • 1. Java 不是 javascript。 2. 谷歌搜索“read file java”。 3. 再谷歌搜索“split line java”。 4. 你通过一点研究解决了你的问题。
  • If needed I can send you the txt file..没必要,只显示你到目前为止尝试过的内容。
  • 大家好,感谢 cmets。老实说,我已经尝试了 4 个小时,但似乎找不到解决方案。 (反斜杠对不起标签)
  • 代码更好!但是,如果您将问题改写为与您遇到的问题相匹配,您就会得到结果:我相信您在“拆分字符串”时遇到了问题...
  • 嘿 ppeterka,谢谢。我想我不知道这个问题的正确标题是什么。我会尝试用你的建议多用谷歌搜索一下,看看我明白了。

标签: java bufferedreader filereader


【解决方案1】:

以下代码正在使用您提供的示例数据。

简而言之

  1. 创建一个字段来存储您的数据,我选择了TreeMap,这样您就可以将一个字母映射到一定数量的整数,但您可以使用另一个Collection

  2. 使用BufferedReader#readLine()逐行读取文件

  3. 然后根据您的数据处理每一行。在这里,我使用regular expressions 匹配给定的行,然后删除所有不是数据的内容。见String#split()String#matches()

首先首先要阅读一些关于 Java 和面向对象设计的优秀初学者书籍。

public class ReadAndParse {

    public Map<String, ArrayList<Integer>> data = new TreeMap<String, ArrayList<Integer>>();

     public ReadAndParse() {
        try {
            FileReader fr = new FileReader("test.txt");
            BufferedReader br = new BufferedReader(fr);
            while(true) {
                String line = br.readLine();
                if (line == null)    break;

                else if (line.matches("\\\\#\\s[a-zA-Z]")){
                    String key = line.split("\\s")[1];
                    line = br.readLine();

                    ArrayList<Integer> value=  computeLine(line);

                    System.out.println("putting key : " + key + " value : " + value);
                    data.put(key, value);
                }
                else if (line.matches("\\\\\\#\\s(\\|[a-zA-Z]\\|,?\\s?)+")){
                    String[] keys = line.split("\\s");
                    line = br.readLine();

                    ArrayList<Integer> results = computeLine(line);

                    for (int i=1; i<keys.length; i++){
                        keys[i] = keys[i].replace("|", "");
                        keys[i] = keys[i].replace(",", "");

                        System.out.println("putting key : " + keys[i] + " value : " + results.get(i-1));

                        ArrayList<Integer> value=  new ArrayList<Integer>();
                        value.add(results.get(i-1));
                        data.put(keys[i],value);
                    }
                }
            }

        } 
        catch (IOException e) {
            e.printStackTrace();
        }

        // print the data
        for (Entry<String, ArrayList<Integer>> entry : data.entrySet()){
            System.out.println("variable : " + entry.getKey()+" value : "+ entry.getValue() );
        }
}

    // the compute line function
    private ArrayList<Integer> computeLine(String line){
        ArrayList<Integer> integerList = new ArrayList<>();
        String[] splitted = line.split("\\s+");
        for (String s : splitted) {
            System.out.println("Compute Line : "+s);
            integerList.add(Integer.parseInt(s));
        }
        return integerList;
    }

    // and the main function to call it all
public static void main(String[] args) {
    new ReadAndParse();
}
}

解析您的文件后得到的一些示例输出:

 variable : L value : [30]
 variable : S value : [1]
 variable : T value : [38]
 variable : b value : [1, 1, 1, 1, 1]
 variable : c value : [1399, 1563, 1303, 1303, 2019]
 variable : v value : [8213, 9319, 10187, 12144, 8206]
 variable : w value : [7027, 9652, 9956, 13973, 6661, 14751]

【讨论】:

  • 嗨,马克,谢谢您的快速回复。但是,我在让它工作时遇到了一些麻烦。我刚刚在问题描述中添加了一个数据示例。你能举个例子吗?提前谢谢。 N
  • 您需要修改 computeLine 函数。让我看看我能不能用你的数据做点什么
  • @MetaNa 尝试了一些东西来让您领先一步。我没有测试过,所以谨慎使用。但它应该让你知道该怎么做。如果没有,请随时告诉我。
  • 嗨,马克,哇,你的速度超级快!我已尝试实施您的建议。但不知何故,它似​​乎不起作用。我在上面插入了“新代码”。有什么建议,我做错了什么吗?干杯,N
  • 如果你不告诉我出了什么问题,我无法告诉你。正如我所说的我还没有测试过代码,我会运行它并查看...
【解决方案2】:

我想我得到了一些东西。

编辑:

我改变了我的方法

您需要导入;

import java.io.BufferedReader;

然后

BufferedReader reader = new BufferedReader

int[] arr = new int[3];
int L;
int T;
int S;
for (int i = 0 ;i<5; i++){ //brings you to fifth line

 line = reader.readLine();
}
L = line.split(" ")[0]trim();
T = line.split(" ")[1]trim();
S = line.split(" ")[2]trim();

arr[0] = (L);
arr[1] = (T);    
arr[2] = (S);    

【讨论】:

  • 你在发明代码吗? int Array arr = new Array [3]; 是什么?
  • 您好罗伯特,谢谢您的回复。我添加了上面代码的示例。你能举个例子吗?提前谢谢。 N
猜你喜欢
  • 2013-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-09
  • 1970-01-01
  • 2021-03-23
  • 2013-11-19
相关资源
最近更新 更多