我在尝试读取资源文件夹中的文件时出现问题。有问题的图书馆是; Web3j的钱包实用程序类。由于某种原因,它不会找到json文件。 json文件位于assets文件夹中。
currentWallet = WalletUtils.loadCredentials(password , "/some.json");
堆栈跟踪如下;
W/Java7Support: Unable to load JDK7 types (annotations, java.nio.file.Path): no Java7 support added
E/Main Activity: onCreate:
java.io.FileNotFoundException: keystore.json (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:756)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2740)
at org.web3j.crypto.WalletUtils.loadCredentials(WalletUtils.java:81)
at org.web3j.crypto.WalletUtils.loadCredentials(WalletUtils.java:76)
at mecreative.studio.liberaltoken.MainActivity.handleCreateContract(MainActivity.java:126)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6199)
at android.widget.TextView.performClick(TextView.java:11090)
at android.view.View$PerformClick.run(View.java:23647)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
我想明确说明,方法Wallet #loadCredentials接受一个字符串,后跟File对象或String对象。我试过了两个,他们都给出了同样的错误。
答案 0 :(得分:0)
private BufferedReader file = new BufferedReader(new InputStreamReader(getAssets().open("file.json")));
答案 1 :(得分:0)
public static String getJSONFileFromAsset(String filename, Context context) throws IOException {
AssetManager manager = context.getAssets();
InputStream file = manager.open(filename);
byte[] formArray = new byte[file.available()];
file.read(formArray);
file.close();
return new String(formArray);
}
这应该有效
答案 2 :(得分:0)
从文件中读取数据的其他选项
public String ReadDataFromFile(String file) {
String data = "";
try {
InputStream stream = getAssets().open(file);
int size = stream.available();
byte[] buffer = new byte[size];
stream.read(buffer);
stream.close();
data = new String(buffer);
} catch (IOException e) {
// Handle exceptions here
}
return data;
}
并使用
String yourData = ReadDataFromFile("YourDataFile.json");