I have a method to return a string of the folder path. CSVFile method calls another method ConfigFilePath to extract the value from a config file and return it back to be used for another condition.
我有一个方法来返回文件夹路径的字符串。 CSVFile方法调用另一个方法ConfigFilePath从配置文件中提取值并将其返回以用于另一个条件。
public CSVFile(ManagedElement_T[] meInfo) {
String path = null;
ConfigFilePath(path);
system.out.print("CSV "+path);
}
public String ConfigFilePath(String path) {
final String directory = System.getProperty("user.dir");
final String filename = "config.properties";
final File configFile = new File(directory+"/"+filename);
final Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(configFile);
prop.load(input);
path = prop.getProperty("diretory_csv");
System.out.println("PATH " + path);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
// Close the file
}
File checkPath = new File(path.toString());
System.out.println("CHECK " + checkPath);
if (checkPath.exists()) {
System.out.println("Directory already exists ...");
} else {
//mkdir if it does not exist
}
return path;
}
Problem is, it doesn't return any var path? just says null when I print it but inside the ConfigFilePath method it seems its getting the right values based from the prints in eclipse.
问题是,它没有返回任何var路径?当我打印它时只是说null但是在ConfigFilePath方法中它似乎从eclipse中的打印得到了正确的值。
System.out.println("PATH " + path); = C:/opt/CORBA/input
System.out.println("CHECK " + checkPath); = C:\opt\CORBA\input
System.out.print("CSV "+path); = null
4 个解决方案
#1
2
Java is actually pass by value
not pass by reference
.
Java实际上是通过值传递而不是通过引用传递。
so when you initialized it here:
所以当你在这里初始化时:
path = prop.getProperty("diretory_csv");
path wont actually reference
that value but get destroyed when out of scope
.
path实际上不会引用该值,但在超出范围时会被销毁。
solution:
Get the returned value of the ConfigFilePath
method
获取ConfigFilePath方法的返回值
path = ConfigFilePath(path);
#2
6
The method ConfigFilePath
is returning a value. You have to assign that returned value to the variable path
:
ConfigFilePath方法返回一个值。您必须将返回的值分配给变量路径:
path = ConfigFilePath(path);
Note: Try to follow Java naming conventions. Use mixedCase
for methods/variables and use CamelCase
for classes/interfaces
注意:尝试遵循Java命名约定。将mixedCase用于方法/变量,并将CamelCase用于类/接口
#3
0
ConfigFilePath is returning a value. You should get the result from it and put it in the path:
ConfigFilePath返回一个值。您应该从中获取结果并将其放在路径中:
public CSVFile(ManagedElement_T[] meInfo) {
String path = ConfigFilePath(path);
system.out.print("CSV "+path);
}
#4
0
The problem you face is that java is passing method-parameters by value, not by reference. It can be confusing, since when using object types the passed value is actually an object-reference.
您遇到的问题是java是按值传递方法参数,而不是通过引用传递。它可能令人困惑,因为在使用对象类型时,传递的值实际上是对象引用。
Let's have a look at some lines of your code:
我们来看看代码的某些行:
public String ConfigFilePath(String path)
Here your method gets passed a reference to some String
and keeps it in a local variable called path
.
在这里,您的方法传递对某些String的引用,并将其保存在名为path的局部变量中。
path = prop.getProperty("diretory_csv");
Now you're changing the local variable path
to point to a new String
-object.
现在,您要将局部变量路径更改为指向新的String对象。
return path;
This is passing out the reference as a return-value. ConfigFilePath
's local path
-variable goes out of scope.
这会将引用作为返回值传递出去。 ConfigFilePath的本地路径变量超出范围。
ConfigFilePath(path);
As the other answers said: in this line you're calling the method passing a reference to a String
that is referenced by CSVFile
's local path
-variable. But since passing is by value this path
-variable will not be changed by the method-call. Your result is passed via the return
-value - and you are ignoring that instead of assigning it to your path
-variable.
正如其他答案所说:在这一行中,您调用的方法是将引用传递给由CSVFile的本地路径变量引用的String。但由于传递是按值,因此方法调用不会更改此路径变量。您的结果通过返回值传递 - 您忽略了它而不是将其分配给路径变量。
So to sum it up:
总结一下:
-
ConfigFilePath()
doesn't need a parameter since it's not using the value that is passed in - You have to assign the returned value to some variable in your calling method for it to be of any use.
ConfigFilePath()不需要参数,因为它没有使用传入的值
您必须将返回的值分配给调用方法中的某个变量,以使其具有任何用途。
#1
2
Java is actually pass by value
not pass by reference
.
Java实际上是通过值传递而不是通过引用传递。
so when you initialized it here:
所以当你在这里初始化时:
path = prop.getProperty("diretory_csv");
path wont actually reference
that value but get destroyed when out of scope
.
path实际上不会引用该值,但在超出范围时会被销毁。
solution:
Get the returned value of the ConfigFilePath
method
获取ConfigFilePath方法的返回值
path = ConfigFilePath(path);
#2
6
The method ConfigFilePath
is returning a value. You have to assign that returned value to the variable path
:
ConfigFilePath方法返回一个值。您必须将返回的值分配给变量路径:
path = ConfigFilePath(path);
Note: Try to follow Java naming conventions. Use mixedCase
for methods/variables and use CamelCase
for classes/interfaces
注意:尝试遵循Java命名约定。将mixedCase用于方法/变量,并将CamelCase用于类/接口
#3
0
ConfigFilePath is returning a value. You should get the result from it and put it in the path:
ConfigFilePath返回一个值。您应该从中获取结果并将其放在路径中:
public CSVFile(ManagedElement_T[] meInfo) {
String path = ConfigFilePath(path);
system.out.print("CSV "+path);
}
#4
0
The problem you face is that java is passing method-parameters by value, not by reference. It can be confusing, since when using object types the passed value is actually an object-reference.
您遇到的问题是java是按值传递方法参数,而不是通过引用传递。它可能令人困惑,因为在使用对象类型时,传递的值实际上是对象引用。
Let's have a look at some lines of your code:
我们来看看代码的某些行:
public String ConfigFilePath(String path)
Here your method gets passed a reference to some String
and keeps it in a local variable called path
.
在这里,您的方法传递对某些String的引用,并将其保存在名为path的局部变量中。
path = prop.getProperty("diretory_csv");
Now you're changing the local variable path
to point to a new String
-object.
现在,您要将局部变量路径更改为指向新的String对象。
return path;
This is passing out the reference as a return-value. ConfigFilePath
's local path
-variable goes out of scope.
这会将引用作为返回值传递出去。 ConfigFilePath的本地路径变量超出范围。
ConfigFilePath(path);
As the other answers said: in this line you're calling the method passing a reference to a String
that is referenced by CSVFile
's local path
-variable. But since passing is by value this path
-variable will not be changed by the method-call. Your result is passed via the return
-value - and you are ignoring that instead of assigning it to your path
-variable.
正如其他答案所说:在这一行中,您调用的方法是将引用传递给由CSVFile的本地路径变量引用的String。但由于传递是按值,因此方法调用不会更改此路径变量。您的结果通过返回值传递 - 您忽略了它而不是将其分配给路径变量。
So to sum it up:
总结一下:
-
ConfigFilePath()
doesn't need a parameter since it's not using the value that is passed in - You have to assign the returned value to some variable in your calling method for it to be of any use.
ConfigFilePath()不需要参数,因为它没有使用传入的值
您必须将返回的值分配给调用方法中的某个变量,以使其具有任何用途。