在 java 中读取具有不同开始和结束分隔符的文件
Reading file with different start and end delimiters in java
我正在尝试实施会议安排算法。我想随机生成会议并将其存储在文件中。然后用另一个代码读取这个文件,创建将尝试安排这些会议的不同代理。
我输入的会议文件如下:
1 20 25 [1, 2, 3, 4, 5] [4, 5]
2 21 29 [1, 6, 7, 5, 33] [1, 5, 33]
从左到右,这些值分别表示会议 ID、开始时间、硬截止日期、与会者 ID 列表、基本与会者 ID 列表。
基本上是整数和整数数组列表的组合(动态的,大小不固定)。
为了存储这个,我使用了这个代码
File fleExample = new File("Meeting.txt")
PrintWriter M1 = new PrintWriter(fleExample);
M1.print(m.getMeetingID()+" "+m.getStartTime()+" "+m.getHardDeadLine()+" "+m.getAttendees()+" "+m,getEssentialAttendees());
M1.println();
我想读取这些值并将其设置为整型变量和整型数组列表。
FileInputStream fstream = new FileInputStream("Meeting.txt");
DataInputStream inp = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(inp));
String strLine;
while ((strLine = br.readLine()) != null) {
String[] tokens = strLine.split(" ");
for (int i = 0; i < MeetingCount; i++) {
Meeting meet = new Meeting();
meet.setMeetingID(Integer.valueOf(tokens[0]));
meet.setStartTime(Integer.valueOf(tokens[1]));
meet.setHardDeadLine(Integer.valueOf(tokens[2]));
}
}
我可以将值设置为整数,但找不到方法来为arraylist.I做同样的事情,想将字符串存储到arraylist。任何这方面的帮助会很棒。
String fileinput="2 21 29 [6 7] [71 45 33]";
Pattern p=Pattern.compile("[0-9]+");
Matcher m=p.matcher(fileinput);
while (m.find()) {
int i=Integer.parseInt(fileinput.substring(m.start(), m.end()));
System.out.println(i);
}
以上问题通过使用正则表达式解决,它连续搜索一个或多个整数并在找不到更多整数时中断。
这个过程将重复直到字符串结束。 m.find 将 returns 识别模式的开始和结束位置。我们使用起始值和结束值从主字符串中提取子字符串,然后解析为整数。
我不确定你的实现是为了什么(以及 Meeting
对象是关于什么的),但如果你只想将它们分配给 int 或 list 变量,请尝试使用扫描仪和一一阅读:
String str = "1 20 25 [1 2 3] [4 5]";
Scanner scan = new Scanner(str);
int intVariable = 0;
ArrayList<Integer> listVariable = null; //null marks no active list
while (scan.hasNext()) { //try/catch here is highly recommeneded!
//read next input (separated by whitespace)
String next = scan.next();
if (next.startsWith("[")) {
//init new list and store first value into it
listVariable = new ArrayList<Integer>();
listVariable.add(Integer.parseInt(next.substring(1)));
} else if (next.endsWith("]")) {
//add the last item to the list
listVariable.add(Integer.parseInt(next.substring(0, next.length()-1)));
System.out.println(Arrays.toString(listVariable.toArray()));
//reset the list to null
listVariable = null;
} else {
//if inside a list, add it to list, otherwise it is simply an integer
if (listVariable != null) {
listVariable.add(Integer.parseInt(next));
} else {
intVariable = Integer.parseInt(next);
System.out.println(intVariable);
}
}
}
这里我只是简单地打印了输出,但是你当然可以将它投影到任何你需要的地方,或者有一个整数值列表和一个整数列表值列表。
另请注意,在本示例中我只截取了文件的一行,但您可以直接将文件提供给扫描仪(无需自己逐行读取)。
希望对您有所帮助。
我正在尝试实施会议安排算法。我想随机生成会议并将其存储在文件中。然后用另一个代码读取这个文件,创建将尝试安排这些会议的不同代理。
我输入的会议文件如下:
1 20 25 [1, 2, 3, 4, 5] [4, 5]
2 21 29 [1, 6, 7, 5, 33] [1, 5, 33]
从左到右,这些值分别表示会议 ID、开始时间、硬截止日期、与会者 ID 列表、基本与会者 ID 列表。
基本上是整数和整数数组列表的组合(动态的,大小不固定)。 为了存储这个,我使用了这个代码
File fleExample = new File("Meeting.txt")
PrintWriter M1 = new PrintWriter(fleExample);
M1.print(m.getMeetingID()+" "+m.getStartTime()+" "+m.getHardDeadLine()+" "+m.getAttendees()+" "+m,getEssentialAttendees());
M1.println();
我想读取这些值并将其设置为整型变量和整型数组列表。
FileInputStream fstream = new FileInputStream("Meeting.txt");
DataInputStream inp = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(inp));
String strLine;
while ((strLine = br.readLine()) != null) {
String[] tokens = strLine.split(" ");
for (int i = 0; i < MeetingCount; i++) {
Meeting meet = new Meeting();
meet.setMeetingID(Integer.valueOf(tokens[0]));
meet.setStartTime(Integer.valueOf(tokens[1]));
meet.setHardDeadLine(Integer.valueOf(tokens[2]));
}
}
我可以将值设置为整数,但找不到方法来为arraylist.I做同样的事情,想将字符串存储到arraylist。任何这方面的帮助会很棒。
String fileinput="2 21 29 [6 7] [71 45 33]";
Pattern p=Pattern.compile("[0-9]+");
Matcher m=p.matcher(fileinput);
while (m.find()) {
int i=Integer.parseInt(fileinput.substring(m.start(), m.end()));
System.out.println(i);
}
以上问题通过使用正则表达式解决,它连续搜索一个或多个整数并在找不到更多整数时中断。 这个过程将重复直到字符串结束。 m.find 将 returns 识别模式的开始和结束位置。我们使用起始值和结束值从主字符串中提取子字符串,然后解析为整数。
我不确定你的实现是为了什么(以及 Meeting
对象是关于什么的),但如果你只想将它们分配给 int 或 list 变量,请尝试使用扫描仪和一一阅读:
String str = "1 20 25 [1 2 3] [4 5]";
Scanner scan = new Scanner(str);
int intVariable = 0;
ArrayList<Integer> listVariable = null; //null marks no active list
while (scan.hasNext()) { //try/catch here is highly recommeneded!
//read next input (separated by whitespace)
String next = scan.next();
if (next.startsWith("[")) {
//init new list and store first value into it
listVariable = new ArrayList<Integer>();
listVariable.add(Integer.parseInt(next.substring(1)));
} else if (next.endsWith("]")) {
//add the last item to the list
listVariable.add(Integer.parseInt(next.substring(0, next.length()-1)));
System.out.println(Arrays.toString(listVariable.toArray()));
//reset the list to null
listVariable = null;
} else {
//if inside a list, add it to list, otherwise it is simply an integer
if (listVariable != null) {
listVariable.add(Integer.parseInt(next));
} else {
intVariable = Integer.parseInt(next);
System.out.println(intVariable);
}
}
}
这里我只是简单地打印了输出,但是你当然可以将它投影到任何你需要的地方,或者有一个整数值列表和一个整数列表值列表。
另请注意,在本示例中我只截取了文件的一行,但您可以直接将文件提供给扫描仪(无需自己逐行读取)。
希望对您有所帮助。