简体   繁体   English

请帮助我理解这一行代码

[英]Please help me understand this line of code

CSVParser csvParser = null;
String csvFilename = "C:\\Users\\ITEM.txt20141022";


csvParser = new CSVParser(new BufferedReader(new FileReader(csvFilename)), CSVFormat.newFormat('|'));

The algorithm as far as I understand it: 据我了解的算法:

  1. The FileReader object is passed to the BufferedReader constructor. FileReader对象将传递给BufferedReader构造函数。
  2. The BufferedReader object is passed to the CSVParser constructor. BufferedReader对象传递给CSVParser构造函数。 This would be one of the two parameters passed to the csvParser? 这将是传递给csvParser的两个参数之一吗?
  3. the second csvParser parameter is CSVFormat.newFormat('|') 第二个csvParser参数是CSVFormat.newFormat('|')

Would this line be considered two parameters that are passed to the csvParser Object? 将此行视为传递给csvParser对象的两个参数吗? These two parameters are then used in the csvParser constructor? 然后在csvParser构造函数中使用这两个参数吗? I have looked at the javadocs and am failing to understand how all these pieces fit together to instantiate the CSVParser. 我看过javadocs,却没能理解所有这些部分如何组合在一起以实例化CSVParser。 How would you know this is possible in the first place? 您首先怎么知道这是可能的? I have been taking a beginner java class for a couple of weeks now and I've never seen anything like this. 我已经上了几周的初学者Java课程,但是我从未见过这样的事情。 Could someone please explain to me how this line works as if you were talking to a 5 year old. 有人可以向我解释一下这条线的工作方式,就像您正在与5岁的孩子聊天一样。

You have it correct, as near as I can tell. 据我所知,您说的没错。

This programmer is invoking constructors in place of declaring objects of correct type, instantiating objects to put in the object variables, and then using the variables. 该程序员正在调用构造函数,而不是声明正确类型的对象,实例化对象以放入对象变量,然后使用变量。 It could also be written as: 它也可以写成:

CSVFormat format = CSVFormat.newFormat('|');
FileReader reader = new FileREader(csvFilename);
BufferedReader bReader = new BufferedReader(reader);
CSVParser parser = new CSVParser(bReader, format);

I'm assuming the type returned by the first method call. 我假设第一个方法调用返回的类型。

Hope that's more clear. 希望更清楚。 It's how I would have written it... 我就是这样写的...

CSVParser constructor takes two arguments as below: CSVParser构造函数采用两个参数,如下所示:

CSVParser(Reader reader, CSVFormat format)

Now follow the code carefully, should be easy to understand, take care of the parentheses 现在,请仔细遵循代码,应该易于理解,注意括号

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

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