[英]How do I parse through a CSV (comma separated file) and display output
Does anyone know how I would go about creating an application that will go through a comma separated file (CSV) and organize that information and subsequently put that into a database? 有谁知道我将如何创建一个应用程序,该应用程序将通过逗号分隔文件(CSV)进行组织,然后将该信息放入数据库中? For example, I have to go through a file that looks like this:
例如,我必须浏览一个看起来像这样的文件:
Name of Athlete, Birthday, Salary($)
John Morris, 9/25/1991, 5000000
Mike Hawk, 2/2/1988, 1000000
Dewayne Johnson, 11/22/1985, 4000000
etc. 等等
How would I organize this information and set it up? 我将如何组织这些信息并进行设置? I am trying to make it so I can ask the user for input.
我正在努力做到这一点,所以我可以要求用户输入。 For example: have the user enter "a" for amount of salary and output all the players who make that EXACT amount of money.
例如:让用户输入“ a”作为薪水金额,并输出所有赚取该确切金额的玩家。
Player 1 name, Player 1 birthday Player 2 name, Player 2 birthday 玩家1的名字,玩家1的生日玩家2的名字,玩家2的生日
Any help is appreciated. 任何帮助表示赞赏。 I would appreciated help with reading the information and storing/retrieving it.
我会很乐意帮助您阅读信息并存储/检索它。
Don't give me the complete answer. 不要给我完整的答案。 I am a student learning this and I want to understand it.
我是一名学习此内容的学生,我想了解它。
I would think about these steps: 我会考虑以下步骤:
Athlete
class with attributes name
, birthday
and salary
(it's up to you define each data type). name
, birthday
和salary
属性的Athlete
类(由您定义每种数据类型)。 Scanner
or BufferedReader
(using a FileReader
) to accomplish this. Scanner
或BufferedReader
(使用FileReader
)来完成此操作。 Athlete
object and parse each string into an attribute to set it into the object. Athlete
对象,并将每个字符串解析为一个属性,以将其设置为对象。 Athlete
in a collection ( List
or something). Athlete
存储在集合中( List
或类似内容)。 Athlete
s, save them into your database. Athlete
,将它们保存到数据库中。 Splitting by a comma is a naive way of dealing with CSV. 用逗号分割是处理CSV的一种幼稚方式。 Unless you are 100% sure you will never have a comma in an actual field, then go this route, but if you may ever encounter a string like
hello, my name is devshorts
, you should use a CSV library. 除非您100%确定实际字段中永远不会有逗号,否则请
hello, my name is devshorts
此路线,但是,如果您可能遇到像hello, my name is devshorts
这样的字符串hello, my name is devshorts
,则应该使用CSV库。 http://opencsv.sourceforge.net/ seems to be a good choice. http://opencsv.sourceforge.net/似乎是一个不错的选择。
Once thats done, map each entry to a class that represents what a line is. 完成后,将每个条目映射到代表一行的类。 Make an
Atheltes
class, where it has fields for Name
, Birthdate
, and Salary
. 制作一个
Atheltes
类,其中包含Name
, Birthdate
和Salary
字段。
You can use a direct JDBC connection to a sqlite/mysql/whatever database and insert (using raw SQL) the values into a table called Atheletes
. 您可以使用与sqlite / mysql /任何数据库的直接JDBC连接,并将值插入(使用原始SQL)到称为
Atheletes
的表中。 If you want to use an ORM go with hibernate, but that may be more work than you need to set up. 如果要使用ORM,请与休眠一起使用,但这可能比您需要设置的工作还要多。
Well a simple way of reading this type of files would be using a BufferedReader that allows you to read the file line by line. 读取此类文件的一种简单方法是使用BufferedReader,它允许您逐行读取文件。
Then use split function to separate the string by delimiter and convert it into an array. 然后使用split函数通过定界符分隔字符串并将其转换为数组。
With the resulting array you can insert data into the database making proper conversion as required. 使用结果数组,您可以将数据插入数据库,以根据需要进行适当的转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.