Not able to read a file from my assets folder

+1 vote

I am trying to read a file in my assets folder. The library is: Web3j's wallet utility class. I am not able to find the json file though. The json file is located in the assets folder but i can not find it.

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

The stacktrace is as follows:

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)

Please help

Oct 22, 2018 in Blockchain by digger
• 26,740 points
6,950 views

3 answers to this question.

0 votes

You can try this:

private BufferedReader file = new BufferedReader(new InputStreamReader(getAssets().open("file.json")));
answered Oct 22, 2018 by Omkar
• 69,220 points
0 votes

I found another way on the internet to solve this issue:

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");
answered Oct 25, 2018 by Omkar
• 69,220 points
0 votes

Hi.. I faced a similar problem in my project, following is the solution that worked for me:

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);
}
answered Oct 25, 2018 by Shrikanth

Related Questions In Blockchain

0 votes
1 answer

Not Able to register a user with Hyperledger-Fabric v1.1 preview

The error: "Certificate not found with AKI 'e729224e8b3f31784c8a93c5b8ef6f4c1c91d9e6e577c45c33163609fe40011' ...READ MORE

answered Jun 16, 2018 in Blockchain by charlie_brown
• 7,720 points
1,877 views
0 votes
1 answer

Not able to execute a deployed contract?

The problem is that you are not ...READ MORE

answered Jul 30, 2018 in Blockchain by digger
• 26,740 points
713 views
0 votes
1 answer

Not able to run Bitcoin’s autogen.sh file.

Installing autoconf and automake would solve the ...READ MORE

answered Aug 22, 2018 in Blockchain by slayer
• 29,370 points
1,292 views
0 votes
1 answer

How to read file from subprocess.open in python?

you call: out, err = p.communicate() READ MORE

answered Aug 24, 2018 in Blockchain by digger
• 26,740 points
1,461 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,922 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,427 views
0 votes
1 answer

Hyperledger composer: Not able to issue identities from a Business network.

To specify your own affiliation you need ...READ MORE

answered Nov 6, 2018 in Blockchain by Omkar
• 69,220 points
1,447 views
0 votes
1 answer

Not able to invoke a contract in Ethereum Private chain using geth

Hope this helps: contract mortal { /* ...READ MORE

answered Oct 22, 2018 in Blockchain by Omkar
• 69,220 points
979 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP