简体   繁体   English

读取错误的 CSV 文件并将“坏行”打印到另一个文件 Java

[英]Reading a CSV file with errors and printing the 'bad lines' to another file Java

So my assignment is to read a csv file containing 'employee' objects.所以我的任务是读取包含“员工”对象的 csv 文件。 This file contains errors within it, for example a normal csv line should read:此文件中包含错误,例如正常的 csv 行应为:

EmployeeId,FirstName,LastName,DOB,Gender,Position员工 ID、名字、姓氏、出生日期、性别、Position

Some lines of the file however are either in the wrong format or contain null values.然而,文件的某些行格式错误或包含 null 值。 Eg:例如:

9658273,bad 9658273,坏

I need to be able to read the file and use exceptions to identify where the errors occur and write these 'bad lines' to a new file called bad.txt .我需要能够读取文件并使用异常来识别错误发生的位置并将这些'bad lines'写入一个名为bad.txt的新文件。

I am unsure where to start at this point and need some direction.我不确定从哪里开始,需要一些方向。 I know how to read a file that has no errors but I'm having trouble writing exceptions.我知道如何读取没有错误的文件,但我在编写异常时遇到了麻烦。

So the first thing you should look at is what constitutes a 'bad line'.因此,您应该首先查看的是什么构成了“坏线”。 Write up some if statements for this:为此写一些 if 语句:

For example (this is just logic/pseudocode):例如(这只是逻辑/伪代码):

if i have a null line:
write this line into the error file
else if i have a line with first name before employeeid:
write this line into the error file
etc, etc

You will need to figure out how to tell when a entry is out of order, which will probably be looking at types.您将需要弄清楚如何判断条目何时出现故障,这可能会查看类型。 For instance, name should not be a date/time.例如,名称不应该是日期/时间。 While Gender should not be an ID of 1028473AA848737.而 Gender 不应该是 1028473AA848737 的 ID。

Don't worry too much about writing the error into error.txt for now.现在不要太担心将错误写入error.txt。 Start with a simple:从一个简单的开始:

if i detect an error: 
print out "ERROR DETECTED" 

then once you have a case where you can see you are detecting the errors correctly, you can move onto writing them to a file.然后,一旦遇到可以看到正确检测到错误的情况,就可以继续将它们写入文件。

Hope this might give you something to start off: Let me know if you need further guidance :)希望这可能会给您一些帮助:如果您需要进一步的指导,请告诉我 :)

You can see: https://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/ to read data, then validate them您可以看到: https://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/读取数据,然后验证它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM