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

  • Home
  • SEARCH
  • 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 8842413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:57:08+00:00 2026-06-14T10:57:08+00:00

I have the following code and tries to store an array in a .data

  • 0

I have the following code and tries to store an array in a .data file. Problem is that I don’t know how to do this, I get a filenotfoundException when trying to create a file.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class SimpleBookManager implements BookManager, java.io.Serializable {

    private ArrayList<Book> allBooks = new ArrayList<Book>();


    public ArrayList<Book> getAllBooks(){
        return  allBooks;
    }

    public void saveChanges(){
        try{
            FileOutputStream f_out = new FileOutputStream("myobject.data");
            // Write object with ObjectOutputStream
            ObjectOutputStream obj_out = new
                ObjectOutputStream (f_out);

            // Write object out to disk
            obj_out.writeObject(allBooks);
        }catch (FileNotFoundException e) {
            System.out.println("No file found saveChanges");//Why can't I create a new "myObject.data"?
        } catch (IOException e) {
            //someCode();
        } 
    }

    public void loadBooks(){
        try{
        // Read from disk using FileInputStream
        FileInputStream f_in = new 
            FileInputStream("myobject.data");

        // Read object using ObjectInputStream
        ObjectInputStream obj_in = 
            new ObjectInputStream (f_in);

        // Read an object
        Object obj = obj_in.readObject();

        if (obj instanceof ArrayList<?>)
        {
            // Cast object to a Vector
            allBooks = (ArrayList<Book>) obj;
            System.out.println("Worked");

        }
        }catch (FileNotFoundException e) {
            System.out.println("No file found LoadBooks");
        } catch (IOException e) {
            //someCode();
        } 
        catch (ClassNotFoundException e) {
            //someCode();
        }

    }


      public static SimpleBookManager getSimpleBookManager()
      {
        if (ref == null){
            ref = new SimpleBookManager();
        }

        return ref;
      }

    public SimpleBookManager(){

    }
}

Problem is that it throws filenotfound exeption both when I try to create it and later read it, why? Can’t I create .data files on android emulator?
_______________________________________________________

Edit here’s my manifest and added the <uses permission> at the end of the file but it still can’t create a file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="edu.chalmers.BookApp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="16" />

    <application
        android:icon="@drawable/ic_launcher"

        >
        <activity
            android:name=".BookApp"
            android:label="@string/title_bookapp"
            android:uiOptions="splitActionBarWhenNarrow" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
            <activity
        android:name=".AddBookActivity"
        android:uiOptions="splitActionBarWhenNarrow" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="edu.chalmers.BookApp" />
    </activity>
     <activity
        android:name=".DetailActivity"
        android:uiOptions="splitActionBarWhenNarrow" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="edu.chalmers.BookApp" />
    </activity>
     <activity
        android:name=".EditBookActivity"
        android:uiOptions="splitActionBarWhenNarrow" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="edu.chalmers.BookApp" />
    </activity>

    </application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
</manifest>
  • 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-14T10:57:09+00:00Added an answer on June 14, 2026 at 10:57 am

    Change the saveChanges() method to take a Context as a parameter…

    public void saveChanges(Context context) {...}
    

    Then in saveChanges(...) try changing this line…

    FileOutputStream f_out = new FileOutputStream("myobject.data");
    

    …to…

    FileOutputStream f_out = context.openFileOutput("myobject.data", Context.MODE_PRIVATE);
    

    Also pass a Context to loadBooks(...) and change this line…

    FileInputStream f_in = new FileInputStream("myobject.data");
    

    …to…

    FileInputStream f_in = context.openFileInput("myobject.data");
    

    When calling either method from an Activity you would use this as the context. Example…

    myBookManager.saveChanges(this);
    myBookManager.loadBooks(this);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How is an array saved to NSUserDefaults? I have the following code which tries
I am currently working on Problem 62 I have tried the following code to
I have the following code to optimize. As I expect the file to be
The Background I have the following source code #include <libubuntuone-1.0/u1-music-store.h> #include <libsyncdaemon-1.0/libsyncdaemon/libsyncdaemon.h> static void
I have an array that can store numbers in such as 01, 01A, 01B,
Anything I have tried didn't work. Currenly I have following code to change asp.net
I have tried the following code but has no effect: Imports system.Runtime.InteropServices <DllImport(UxTheme.DLL, BestFitMapping:=False,
I have tried the following code: $('a.buildMenu').click(function (event) { // Prevent normal behaviour event.preventDefault();
I have tried for unload and beforeunload binding in following code, but none of
I have tried to use the following snippet of code: int main() { string

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.