简体   繁体   English

如何将文件名字符串传递给另一个 Class 以便它在该 class 中创建文件 object?

[英]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.

相关问题 如何将 object 数组传递给另一个 class 中的方法,以便此方法将其用作普通数组? - How can I pass an object array to a method in another class so this method uses it as a normal array? 我如何将请求对象从一个类传递到另一个类 - how can i pass request object from one class to another 如何将对象类型的数组存储为字符串,以便另一个类以后可以调用并打印该字符串。 爪哇 - How to store an array of object types as a string so another class can later call and print that string. java 如何将类的对象传递给另一个类的方法? - How can pass an object of a class to another class's method? 我如何将表导入另一个类(/ object ??),以便可以在另一个类/对象中对它运行查询? [slick 3.0] [scala] - How would I import tables into another class (/object??) so I can run queries on it in the other class/object? [slick 3.0][scala] 我如何在另一个类中使用一个文件对象 - how can i use one file object in another class 如何在Java中使用TestNG将字符串从主类传递到另一个类? - How can I pass a string from main class to another class using TestNG in java? 如何在邮递员中传递类对象? - How can I pass a class object in postman? 如何将数据从一个班级传递到另一个班级? - How can i pass data from class to another class? 在UML类图中,如何显示类创建另一个类的对象但不存储对象引用? - In a UML Class Diagram, how do I show that a class creates an object of another class but doesn't store the object reference?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM