简体   繁体   English

使用参数化的类使用Java返回从指定文件读取的数据

[英]use parameterized class to return read data from specified file using Java

What I'm trying to do: During my tests, I have to read data from different files. 我要做什么:在测试期间,我必须从不同的文件中读取数据。 (ID of user, ID of Office and so on. each id in separate file) The Idea to use class read_file(file_name); (用户ID,Office ID等。每个ID在单独的文件中)使用类read_file(file_name)的想法; where file name – it would be name of file, where to read. 文件名-将是文件名,读取位置。 What I have: 我有的:

    public class Read_File {
    public static String client_number;
    private FileReader fr;

public static String read(String file_name){
String fileName=System.getProperty("user.dir")+"/src/resources/data_files/"+file_name+".txt";
try{
    FileReader inputFile = new FileReader(fileName);
    BufferedReader bufferReader = new BufferedReader(inputFile);

   String client_number = bufferReader.readLine();
    System.out.print("client number "+client_number);
   bufferReader.close();


} catch (FileNotFoundException e) {
    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
}
    return client_number;
}
public Read_File (FileReader fr) {
    this.fr = fr;

}
}

I'm trying to run it 我正在尝试运行它

  public void Find_Customer() {
    String client_number;

   Read_File file = new Read_File(fr);
   client_number = file.read("last_customer");
   System.out.print("client number"+client_number);
}

I'm able to read value from file but when I'm trying to get this value from method - I have null. 我能够从文件中读取值,但是当我尝试从方法中获取此值时-我为空。 I'm doning something completely wrong. 我做错了什么。 could you help me to write it correct, please. 你能帮我把它写正确吗?

your method is static. 您的方法是静态的。 you have 2 entities called client_number. 您有2个名为client_number的实体。 One is a static member of the Read_File class and one is String client_number = bufferReader.readLine(); 一个是Read_File类的静态成员,一个是String client_number = bufferReader.readLine(); .

The scope of the local variable ends when the try block ends. try块结束时,局部变量的作用域结束。 You actually read that value but your return the value of the member client_number which is null. 您实际上读取了该值,但是返回成员client_number的值,该值为null。

Try this client_number = bufferReader.readLine(); 试试这个client_number = bufferReader.readLine(); (remove String type in front of this declaration). (删除此声明前面的String类型)。 Also be careful that in case of an exception to return null. 还应注意,如果发生异常,则返回null。

Another solution is to remove the class member client_number (code would look like this): 另一个解决方案是删除类成员client_number(代码如下所示):

public class Read_File {
private FileReader fr;

public static String read(String file_name){
String fileName=System.getProperty("user.dir")+"/src/resources/data_files/"+file_name+".txt";
String client_number = null;
try{
    FileReader inputFile = new FileReader(fileName);
    BufferedReader bufferReader = new BufferedReader(inputFile);

   client_number = bufferReader.readLine();
   System.out.print("client number "+client_number);
   bufferReader.close();


} catch (FileNotFoundException e) {
    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
}
    return client_number;
}
public Read_File (FileReader fr) {
    this.fr = fr;

}
}

As a side note, given that the method read is static you do not need to create an object of type Read_File . 附带说明一下,由于read方法是静态的,因此您无需创建Read_File类型的对象。

Well, your mixing up several concepts of the java language... A quick and dirty fix would look like this: 好吧,您混淆了Java语言的几个概念...一个快速而肮脏的修复程序看起来像这样:

public class Read_File {

    public static String read(String file_name){
        String client_number = "";
        String fileName=System.getProperty("user.dir")+"/src/resources" + 
            "/data_files"+file_name+".txt";
        try{
            FileReader inputFile = new FileReader(fileName);
            BufferedReader bufferReader = new BufferedReader(inputFile);

            String client_number = bufferReader.readLine();
            System.out.print("client number "+client_number);
            bufferReader.close();


        } catch (FileNotFoundException e) {
            e.printStackTrace();  
            //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  
            //To change body of catch statement use File | Settings | File Templates.
        }
        return client_number;
    }
}

Then you simply call it like that: 然后,您可以简单地这样称呼它:

String client_number = Read_File.read("last_customer");

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

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