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 6185515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:43:34+00:00 2026-05-24T01:43:34+00:00

I am having a hard time figuring out how to write to and read

  • 0

I am having a hard time figuring out how to write to and read from files on an Android device. The file will be formatted as XML and I already have parsers and data structures built that can format the XML into objects and objects into XML, but the last hurdle is reading the XML from a non-resource file (I know the data structures work because I it works when reading from a resource file) and also writing to a non-resource file. I am terrible at using tools to debug (not sure how to print a stack trace) but I know for a fact the problem is that I cannot read from or write to this files. I have no experience writing to files in Java which may be why I am having a rough time with this.

Write code:

    File scoresFile = new File(getExternalFilesDir(null), "scores.xml");

    if (!scoresFile.exists())
    {
        scoresFile.createNewFile();
    }

    OutputStream os = new FileOutputStream(scoresFile);

    os.write(writer.toString().getBytes());
    os.flush();
    os.close();

Read Code:

    XmlPullParserFactory xmlFac = XmlPullParserFactory.newInstance();
    XmlPullParser qXML = xmlFac.newPullParser();
    InputStream is = null;
    File scoresFile = new File(c.getExternalFilesDir(null), "scores.xml");

    if (!scoresFile.exists())
    {
        try {
            scoresFile.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    try {
        is = new FileInputStream(scoresFile);
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    if (is != null)
        qXML.setInput(is,null);
    else
        qXML = c.getResources().getXml(R.xml.scores);

UPDATE: The last if clause in the read section always evaluates to false. So, the InputStream is null… that appears to be the root of my problem.

  • 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-05-24T01:43:35+00:00Added an answer on May 24, 2026 at 1:43 am

    I would take a look at these two links: Using Internal Storage and Using External Storage

    Both link to the same page, just different portions. Really, it depends on whether or not you want to save this file to the devices memory, or to an external medium (such as an SDcard).

    1. Internal – Sandboxed, so that only your app can access it.
    2. External – Anyone can access it.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a hard time figuring out where the problem is coming from, so
I'm having a hard time figuring out how to prevent Paperclip from deleting the
I'm having a hard time even figuring out how to write a Google search
I'm having a hard time figuring out how to unzip a zip file with
Having a hard time figuring out the best way to do this... I have
I'm having a hard time figuring out how to architect the final piece of
I'm having a hard time figuring out how Firefox and Chrome determining what fields
I am having a hard time figuring out how to show an Image (or
I am having a hard time figuring out good reasons for the dependency property.
I am having a hard time figuring out how to get the property list

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.