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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:25:36+00:00 2026-05-24T20:25:36+00:00

What is the best way to make a file i’ve created in one activity

  • 0

What is the best way to make a file i’ve created in one activity (say Filewriter.java) to be available(for data manipulation) to all other activities in my app? I’ve made sure to use the same filename but how do i ensure that i’m accessing the one same file everywhere? I need to implement this on 2 files, one containing string names and the other integers. I’ve created the string file using writeUTF();

here is the code for receiving data from a previous activity(the UI) and writing the contents to the file. I want to access this same file from another UI activity…

    package com.shuaib669.bunkrecord;

import java.io.DataOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class filewriter extends Activity{

    String[] newText = new String[7];
    String[] newNum = new String[7];

    int[] no_of_classes = new int[7];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);


        Bundle extras = getIntent().getExtras();
        newText = extras.getStringArray("com.shuaib669.bunkrecord.thetext");
        newNum = extras.getStringArray("com.shuaib669.bunkrecord.thenum");
        finish();

        if (newText != null && newNum != null){

            try {
                      // open subject.txt and classes.bin for writing
                DataOutputStream out1 = new DataOutputStream(openFileOutput("subject.txt", Context.MODE_WORLD_READABLE));
                DataOutputStream out2 = new DataOutputStream(openFileOutput("classses.bin", Context.MODE_WORLD_READABLE));

                for(int x=0;x<7;x++){

                    try{
                //converting string representation of no.s into integers
            no_of_classes[x]=Integer.parseInt(newNum[x]);
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }
                }
                for(int x=0;x<7;x++){

                    if(newText[x]==null)
                        out1.writeUTF("\n");
                    else if(newText[x]==" " || newText[x]=="\n" || newText[x]=="\t")
                        out1.writeUTF("\n");
                    else
                        out1.writeUTF(newText[x]);


                    if(newNum[x]==null)
                        out2.writeInt((Integer) null);
                    else
                        out2.writeInt(no_of_classes[x]);


                }

                out1.close();
            }
                catch(IOException e){
                    Log.e("Data Input Sample", "I/O Error");
                }
              }


    startNextMatchingActivity(new Intent("com.shuaib669.bunkrecord.SUBLIST"));
    //Sublist being the activity that displays the stored contents of the file and lets user manipulate the file classes.bin.       
    }
    }

My main dilemma is to be able to access the SAME one file (whether subject.txt or classes.bin) and to be able to read and manipulate the data from any other activity in the same app.

  • 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-24T20:25:37+00:00Added an answer on May 24, 2026 at 8:25 pm

    I’m not sure what the dilemma is here – if you’re using the same filename, and presumably the same pathname, then by definition – it’s the same file.

    So long as all your activities are in the same app/package, then I don’t see that you would have any problems.

    So – use the same full pathname for each of the two files, and you needn’t worry about whether they are the same file.

    What I would recommend is, instead of using openFileOutput("subject.txt", Context.MODE_WORLD_READABLE) (which is relative, and you may not know where the file is), you probably want to use openFileOutput(getFilesDir ( ) + "/" + "subject.txt", Context.MODE_WORLD_READABLE) which will always provide you with a full pathname within your app’s private data on internal storage.

    I usually provide a helper method in my apps:

    private String getFileName ( String file ) {
        return getFilesDir ( ) + "/" + file;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to make sure that all the environment variables I
I'm wondering what is the best way to make data thread-safe. Specifically, I need
I'm trying to figure out the best way to make a file folder system
What is the best way to make a variable accessable to all classes. For
Possible Duplicate: What's the best way to make a deep copy of a data
How I can make a Makefile, because it's the best way when you distribute
What is the best way to make the size of box 2 equal to
what's the best way to make communications between page widgets residing on differents ViewPart?
What is the best way to make an HTTP GET request in Ruby with
What is the best way to make my Qt Ruby application into an executable

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.