在java中读取文本文件并将数字存储在不同的数组中
我目前正在写我的论文,在这种情况下,我需要使用java开发一个元启发式。然而,我在尝试读取和存储数据时遇到了一个问题 我的文件是一个文本文件,大约有150行。问题的一个例子是第5行,其中说明了三个整数:30、38和1。我想将它们存储为一个分别称为L、T和S的整数,这适用于许多其他行 你们谁知道怎么做?如果需要,我可以给你发送txt文件 顺便说一句:这是我到目前为止尝试过的: Main.java:在java中读取文本文件并将数字存储在不同的数组中,java,bufferedreader,filereader,Java,Bufferedreader,Filereader,我目前正在写我的论文,在这种情况下,我需要使用java开发一个元启发式。然而,我在尝试读取和存储数据时遇到了一个问题 我的文件是一个文本文件,大约有150行。问题的一个例子是第5行,其中说明了三个整数:30、38和1。我想将它们存储为一个分别称为L、T和S的整数,这适用于许多其他行 你们谁知道怎么做?如果需要,我可以给你发送txt文件 顺便说一句:这是我到目前为止尝试过的: Main.java: import java.io.IOException; import java.io.FileWri
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;
}
}
下面的代码正在处理您提供的示例数据 简言之:
TreeMap
,这样可以将一个字母映射到一定数量的整数,但可以使用另一个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();
}
}
我想我得到了一些东西 编辑: 我改变了我的方法 您需要导入
import java.io.BufferedReader;
然后
BufferedReader reader=新的BufferedReader
int[]arr=新的int[3];
int L;
int T;
int-S;
对于(inti=0;i1.Java不是javascript。2.在谷歌上搜索“readfilejava”。3.在谷歌上搜索“splitlinejava”.4.您通过一些研究解决了您的问题。如果需要,我可以将txt文件发送给您。
。无需,只需显示您迄今为止尝试过的内容。嘿,伙计们,谢谢你们的评论。老实说,我已经尝试了4个小时,但似乎找不到解决方案。(反斜杠抱歉标记)使用代码会更好!但是如果您重新表述问题以匹配您的问题,您将得到结果:我相信您在“拆分字符串”时遇到问题…嘿,佩特卡,谢谢。我想我不知道这个问题的正确标题是什么。我会尝试用谷歌搜索一下你的建议,看看我明白了。你是在发明代码吗?什么是int Array arr=new Array[3]
?你好,罗伯特,谢谢你的回复。我在上面添加了一个代码示例。你可以提前给出一个示例吗?谢谢。NHi Marc,谢谢你的快速回复。但是我在让它工作时遇到了一些麻烦。我刚刚在问题描述中添加了一个数据示例。你可以给出一个示例吗?谢谢高级。您需要修改computeLine函数。让我看看是否可以从您的data@MetaNa尝试了一些东西来给你一个启动。我还没有测试过它,所以小心使用。但它应该会让你知道该怎么做。如果没有,请毫不犹豫地告诉我。嗨,马克,哇,你太快了!我已经尝试过实现w你的建议。但不知怎么的,它似乎不起作用。我在上面插入了“新代码”。有什么建议吗,我有什么特别错误吗?干杯,如果你不告诉我出了什么问题,我就不能告诉你。正如我说的,我还没有测试代码,我会运行它,看看。。。
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]
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);