Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:37:02+00:00 2026-06-18T04:37:02+00:00

I would like to store some strings in a simple .txt file and then

  • 0

I would like to store some strings in a simple .txt file and then read them, but when I want to encode them using Base64 it doesn’t work anymore: it writes well but the reading doesn’t work. ^^

The write method:

private void write() throws IOException {
    String fileName = "/mnt/sdcard/test.txt";
    File myFile = new File(fileName);

    BufferedWriter bW = new BufferedWriter(new FileWriter(myFile, true));

    // Write the string to the file
    String test = "http://google.fr";
    test = Base64.encodeToString(test.getBytes(), Base64.DEFAULT);

    bW.write("here it comes"); 
    bW.write(";");
    bW.write(test);
    bW.write(";");

    bW.write("done");
    bW.write("\r\n");

    // save and close
    bW.flush();
    bW.close();
}

The read method :

private void read() throws IOException {
    String fileName = "/mnt/sdcard/test.txt";
    File myFile = new File(fileName);
    FileInputStream fIn = new FileInputStream(myFile);

    BufferedReader inBuff = new BufferedReader(new InputStreamReader(fIn));
    String line = inBuff.readLine();
    int i = 0;
    ArrayList<List<String>> matrice_full = new ArrayList<List<String>>();
    while (line != null) {
        matrice_full.add(new ArrayList<String>());
        String[] tokens = line.split(";");

        String decode = tokens[1];
        decode = new String(Base64.decode(decode, Base64.DEFAULT));

        matrice_full.get(i).add(tokens[0]);
        matrice_full.get(i).add(tokens[1]);
        matrice_full.get(i).add(tokens[2]);
        line = inBuff.readLine();
        i++;
    }
    inBuff.close();
}

Any ideas why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:37:04+00:00Added an answer on June 18, 2026 at 4:37 am

    You have a couple of errors in your code.

    First a couple of notes on your code:

    1. When posting here, attaching a SSCCE helps others to debug your code. This is not a SSCEE because it doesn’t compile. It lacks several defined variables, so one must guess what you really mean. Also you have pasted close-comment token in your code: */ but there is no one start-comment token.
    2. Catching and just suppressing exceptions (like in catch-block in read method) is really bad idea unless you really know what you’re doing. What it does most of the time is hide the potential problems from you. At least write the stacktrace of an exception is a catch block.
    3. Why don’t you just debug it, check what exactly outputs to the destination file? You should learn how to do that because that will speed up your development process, especially for larger projects with hard-to-catch problems.

    Back to the solution:

    1. Run the program. It throws an exception:

      02-01 17:18:58.171: E/AndroidRuntime(24417): Caused by: java.lang.ArrayIndexOutOfBoundsException
      

      caused by line here:

      matrice_full.get(i).add(tokens[2]);
      

      inspecting the variable tokens reveals that it has 2 elements, not 3.

    2. So lets open the file generated by the write method. Doing that shows this output:

      here it comes;aHR0cDovL2dvb2dsZS5mcg==
      ;done
      here it comes;aHR0cDovL2dvb2dsZS5mcg==
      ;done
      here it comes;aHR0cDovL2dvb2dsZS5mcg==
      ;done
      

      Note line breaking here. This is because the Base64.encodeToString() appends additional newline at the end of the encoded string. To generate a one single line, without extra newlines, add Base64.NO_WRAP as the second parameter like this:

      test = Base64.encodeToString(test.getBytes(), Base64.NO_WRAP);
      

      Note here, you must delete file that was created earlier as it has improper line breaking.

    3. Run the code again. It now creates a file with the proper contents:

      here it comes;aHR0cDovL2dvb2dsZS5mcg==;done
      here it comes;aHR0cDovL2dvb2dsZS5mcg==;done
      
    4. Printing the output of matrice_full now gives:

      [
          [here it comes, aHR0cDovL2dvb2dsZS5mcg==, done],
          [here it comes, aHR0cDovL2dvb2dsZS5mcg==, done]
      ]
      

      Note that you’re not doing anything with the value in decode variable in your code, hence the second element is the Base64 representation of that value which is read from the file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to store some information at the request scope when using google
I would like to store some analytics figures. For example, the amount of times
I am playing with neo4j to store some data with JAVA. I would like
We would like to some processing in a Java application, store the results in
For some small programs in Python, I would like to set, store and retrieve
I would like to create a file which stores some data that can be
I would like to load some data stored in a file into my mysql
I would like to store all paths to headers in separate file. I'm going
I have a simple broadcast receiver which receives some data which I would like
I would like to store class object in android sharedpreference. I did some basic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.