>我有以下问题:
我想从另一个类调用一个函数,所以我添加了这行代码
Function1 func = new Function1();
,我收到一个错误说
中的函数 1(上下文(不能应用于 ((
此外,关于这个函数及其错误,我打算调用上述函数,该函数将 JSON 对象和文件名作为参数并返回一个文件,但是,当我输入它时,我收到以下错误
Wrong 2nd argument type, found Java.lang.String required Java.io.File
有问题的代码是这样的:
JSONObject export = jsonArray1.getJSONObject(index);
File file = func.exportToFile(export, "Export.json");
有问题的功能是这样开始的:
public void exportToFile(JSONObject objectToExport, File fN)
{
String output = objectToExport.toString();
file_ = fN;
if (!file_.exists()) {
try {
file_.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try{
FileOutputStream fOut = new FileOutputStream(file_);
fOut.write(output.getBytes());
fOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
注意:我尝试像这样调用函数:
File file = func.exportToFile(export, func.file(;
但我只收到错误说不兼容的类型
必需的 Java.io.file
发现空白
我做错了什么?
这个func.exportToFile(export, func.file);
不会返回任何内容,因为exportToFile
它是一个 void 方法。
更改您的方法以使其以这种方式返回文件:
public File exportToFile(JSONObject objectToExport, File fN) {
String output = objectToExport.toString();
file_ = fN;
if (!file_.exists()) {
try {
file_.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try{
FileOutputStream fOut = new FileOutputStream(file_);
fOut.write(output.getBytes());
fOut.close();
return file_;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}