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); 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. One is a static member of the Read_File class and one is String client_number = bufferReader.readLine();
.
The scope of the local variable ends when the try block ends. You actually read that value but your return the value of the member client_number which is null.
Try this client_number = bufferReader.readLine();
(remove String type in front of this declaration). Also be careful that in case of an exception to return null.
Another solution is to remove the class member client_number (code would look like this):
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
.
Well, your mixing up several concepts of the java language... A quick and dirty fix would look like this:
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");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.