[英]How can I pass a filename String to another Class so that it creates a File object in that class?
I have a file object created in a class called ReadFiles.我有一个文件 object 在名为 ReadFiles 的 class 中创建。
public class ReadFiles {
File file = new File("google.csv");
}
When entered as above with a set "google.csv" the File creation works.如上所述使用一组“google.csv”输入时,文件创建工作。
What I want to do is pass a String filename from another class StockBuySell such that it creates the File in ReadFiles is created based on the String filename.我想要做的是从另一个 class StockBuySell 传递一个字符串文件名,这样它在 ReadFiles 中创建的文件是基于字符串文件名创建的。 Here is what I tried:这是我尝试过的:
public class ReadFiles {
String filename;
public ReadFiles(String filename) {
this.filename = filename;
}
File file = new File(filename);
}
In the other class in the same package:在同一个 package 中的另一个 class 中:
public class StockBuySell {
ReadFiles googleData = new ReadFiles("google.csv");
}
I am given a NullPointerException.我得到一个 NullPointerException。 I believe that is because the file is not created by my method.我相信这是因为该文件不是由我的方法创建的。 What can I do?我能做些什么? Thank you for your help.谢谢您的帮助。
Edit: I realized I was running into errors because of other methods related to reading the files.编辑:我意识到由于与读取文件相关的其他方法,我遇到了错误。 I ended up using hata's method to create a File.我最终使用 hata 的方法来创建一个文件。 Thank you guys!感谢你们!
Is what you want to achieve like this?:你想要达到的目标是这样的吗?:
public class ReadFiles {
File file;
public ReadFiles(String filename) {
file = new File(filename);
}
}
The reason for your NullPointerException should be the field File file = new File(filename);
NullPointerException 的原因应该是字段File file = new File(filename);
. . The filename
is initially null before the Constructor method is called.在调用 Constructor 方法之前, filename
最初是 null。
The order of initialization is a bit wonky;初始化的顺序有点不稳定; first all 'instance initializers' run (and expressions assigned to fields are part of this), and only then the constructor runs.首先运行所有“实例初始化程序”(分配给字段的表达式是其中的一部分),然后才运行构造函数。 Thus:因此:
public class ReadFiles {
private final String filename;
private final File file;
public ReadFiles(String name) {
this.filename = filename;
this.file = new File(filename); // put it here.
}
}
There's all sorts of problems with this code, though.不过,这段代码有各种各样的问题。 It's using old API, and it's got improper naming.它使用的是旧的 API,并且命名不正确。 (This code is reading 1 file, and yet is named 'ReadFiles'. Also, classes should not be named with a verb, it should describe what it represents, not what it does. It also has an additional, mostly useless field. (此代码正在读取 1 个文件,但名为“ReadFiles”。此外,类不应以动词命名,它应该描述它所代表的内容,而不是它的作用。它还有一个额外的,大部分无用的字段。
Fixing all that:解决所有问题:
public class CsvReader {
private final Path file;
public CsvReader(String path) {
this.file = Paths.get(path);
}
public CsvReader(Path path) {
this.path = path;
}
}
and then we run into yet another problem: reading csvs sounds simple but it isn't.然后我们又遇到了另一个问题:读取 csvs 听起来很简单,但事实并非如此。 Use a library such as OpenCSV .使用诸如OpenCSV 之类的库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.