0

I am having an issue with trying to read a file in my assets folder. The library in question is; Web3j's wallet utility class. For some reason it will not find the json file. The json file is located in the assets folder.

currentWallet = WalletUtils.loadCredentials(password , "/some.json");

The stacktrace is the following;

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)

I would like to explicitly state that, the method Wallet#loadCredentials accepts a string followed by either a File object or a String object. I have tried both and they both give the same error.

3 Answers3

0
private BufferedReader file = new BufferedReader(new InputStreamReader(getAssets().open("file.json")));
Nimisha V
  • 461
  • 4
  • 12
  • I only need the File object, I do not need a BufferedReader. – swatarianess Jan 09 '18 at 09:30
  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation would greatly improve its long-term value](//meta.stackexchange.com/q/114762/350567) by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – iBug Jan 09 '18 at 09:57
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);
}

This should work

hemen
  • 1,460
  • 2
  • 16
  • 35
0

Other option to read data from file

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;
}

and Use like

 String yourData = ReadDataFromFile("YourDataFile.json");
Nimisha V
  • 461
  • 4
  • 12