扫描仪和管道分隔文件Java
我正在使用管道分隔的扫描仪和管道分隔文件Java,java,file-io,java.util.scanner,Java,File Io,Java.util.scanner,我正在使用管道分隔的.txt文件处理预定航班。我需要将每一行分成三个独立的部分,scheduledTime,eventType和flightID文件中的一个航班示例如下所示: 12:00 |到达| A001 我有一个单一的航班工作,但当我添加更多,我得到这个错误 完整跟踪: java.time.format.DateTimeParseException: Text 'ARRIVAL' could not be parsed at index 0 at java.time.format.DateT
.txt
文件处理预定航班。我需要将每一行分成三个独立的部分,scheduledTime
,eventType
和flightID
文件中的一个航班示例如下所示:
12:00 |到达| A001
我有一个单一的航班工作,但当我添加更多,我得到这个错误
完整跟踪:
java.time.format.DateTimeParseException: Text 'ARRIVAL' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalTime.parse(Unknown Source)
at java.time.LocalTime.parse(Unknown Source)
at edu.metrostate.ics240.p4.gaw886.sim.AirportSimulator.processEventFile(AirportSimulator.java:49)
at edu.metrostate.ics240.p4.gaw886.sim.tests.AirportSimTests.testScenarioOne(AirportSimTests.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
现在我明白了这里发生的事情,当我有多个航班时,它会跳过第二行中的scheduledTime片段,并尝试解析eventType。我想不出怎么解决这个问题。以下是我的方法:
方法创建事件文件:
public static final double DEPARTURE_PROB = 0.20;
public static final double ARRIVAL_PROB = 0.15;
public static final int ARR_RESERVE_TIME = 2;
public static final int DEP_RESERVE_TIME = 3;
public static final int SIM_MINUTES = 60;
public static final long SEED = 20170620001L;
public static final Random RAND = new Random(SEED);
public static final String DELIM = "\\|";
public static void genEventFile(String fileName, double arrivalProbability, double departureProbability,
int minutes) {
int arrFlightId = 1;
int depFlightId = 1;
try (PrintWriter simFile = new PrintWriter(fileName);) {
LocalTime time = LocalTime.parse("00:00");
for (int i = 0; i < minutes; i++) {
if (RAND.nextDouble() <= arrivalProbability) {
simFile.printf("%s|%s|%s\n", time.plusMinutes(i), Event.EventType.ARRIVAL,
String.format("A%03d", arrFlightId++));
}
if (RAND.nextDouble() <= departureProbability) {
simFile.printf("%s|%s|%s\n", time.plusMinutes(i), Event.EventType.DEPARTURE,
String.format("D%03d", depFlightId++));
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void processEventFile(String filename) {
LocalTime scheduledTime = null;
EventType eventType;
String flightID;
try (Scanner scanner = new Scanner(new File(filename)).useDelimiter(DELIM);) {
while (scanner.hasNext()) {
scheduledTime = LocalTime.parse(scanner.next());
eventType = EventType.valueOf(scanner.next());
flightID = scanner.next();
flight = new Flight(scheduledTime, eventType, flightID);
flightQueue.add(flight);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
因此,我的问题是,如何在读取flightId
后使扫描仪移动到下一行,而不认为第1行的flightId
和第2行的scheduledTime
在同一行
飞行数据样本:
00:02|DEPARTURE|D001
00:03|ARRIVAL|A001
00:03|DEPARTURE|D002
00:04|DEPARTURE|D003
00:06|DEPARTURE|D004
00:09|DEPARTURE|D005
00:12|ARRIVAL|A002
00:14|ARRIVAL|A003
00:16|DEPARTURE|D006
00:18|ARRIVAL|A004
00:21|DEPARTURE|D007
00:22|ARRIVAL|A005
00:24|DEPARTURE|D008
00:28|ARRIVAL|A006
00:28|DEPARTURE|D009
00:30|ARRIVAL|A007
00:41|ARRIVAL|A008
00:44|DEPARTURE|D010
00:47|DEPARTURE|D011
00:48|DEPARTURE|D012
00:53|DEPARTURE|D013
00:54|ARRIVAL|A009
00:54|DEPARTURE|D014
00:56|DEPARTURE|D015
00:57|DEPARTURE|D016
Scanner
对象仅基于DELIM
变量|
解析文件。它也不会按新行字符分割文件
您可以使用BufferedReader
对象逐行读取文件,然后稍后拆分文本。大概是这样的:
public void processEventFile(String fileName) {
String line;
String[] split;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(fileName));
while((line = reader.readLine()) != null) {
split = line.split("\\|");
flightQueue.add(new Flight(split[0], split[1], split[2]));
}
} catch(IOException e) {
e.printStackTrace();
} finally {
if(reader != null) {
try {
reader.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
Scanner
对象仅基于DELIM
变量|
解析文件。它也不会按新行字符分割文件
您可以使用BufferedReader
对象逐行读取文件,然后稍后拆分文本。大概是这样的:
public void processEventFile(String fileName) {
String line;
String[] split;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(fileName));
while((line = reader.readLine()) != null) {
split = line.split("\\|");
flightQueue.add(new Flight(split[0], split[1], split[2]));
}
} catch(IOException e) {
e.printStackTrace();
} finally {
if(reader != null) {
try {
reader.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
您可以将分隔符更改为使用或条件
public static final String DELIM = "[\\||\n]+";
您可以将分隔符更改为使用或条件
public static final String DELIM = "[\\||\n]+";
您可以使用
java.util.Scanner
逐行读取文件。在示例中,我使用try with resources
块确保扫描仪
已关闭。方法forEachRemaining
返回输入文件的下一行:
public static void main(String[] args) throws ParseException {
try (Scanner sc = new Scanner(NewMain.class.getResourceAsStream("airport.txt"))) {
sc.forEachRemaining(s -> {
System.out.println(s.length() + " " + s);
});
}
}
您可以使用
java.util.Scanner
逐行读取文件。在示例中,我使用try with resources
块确保扫描仪
已关闭。方法forEachRemaining
返回输入文件的下一行:
public static void main(String[] args) throws ParseException {
try (Scanner sc = new Scanner(NewMain.class.getResourceAsStream("airport.txt"))) {
sc.forEachRemaining(s -> {
System.out.println(s.length() + " " + s);
});
}
}
你能用一个缓冲读取器吗?然后您可以一次读取一行,并使用String.split(regex)将每行拆分为其组件扫描您发布带有航班id和时间的示例txt文件您可以使用BufferedReader吗?然后你可以一次读取一行,然后使用String.split(regex)将每行拆分为其组件扫描你发布的带有航班id和timeAwesome的示例txt文件,这就解决了问题。Awesome,这就解决了问题。