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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:13:44+00:00 2026-05-30T14:13:44+00:00

code for saving in SD card,i have declared button save where registered onclicklistner as

  • 0

code for saving in SD card,i have declared button save where registered onclicklistner as follows:

        btn.setOnClickListener(new View.OnClickListener()
        {       
    EditText filename =(EditText) findViewById(R.id.filename);
            EditText filecontent =(EditText) findViewById(R.id.filecontent);
       public void onClick(View view) 
        {

        String str = filename.getText().toString();
        String str2= filecontent.getText().toString();

            File sdCard = Environment.getExternalStorageDirectory();
            File directory = new File (sdCard.getAbsolutePath()+"/MyFiles");
            directory.mkdirs();
            File file = new File(directory,"textfile.txt");
                    FileOutputStream fOut = new FileOutputStream(file);
            OutputStreamWriter osw = new OutputStreamWriter(fOut);
                osw.write(str);
            osw.write("   ");
            osw.write(str2);
                    osw.close();


        }

       } 

on load button i have registered onclicklistener while defined block as well
private static final int READ_BLOCK_SIZE = 100;
while load code is:

    Button load = (Button) findViewById(R.id.load);
    load.setOnClickListener(new View.OnClickListener() {
EditText filename =(EditText) findViewById(R.id.filename);
    EditText filecontent =(EditText) findViewById(R.id.filecontent);
    public void onClick(View v) {

       File sdCard = Environment.getExternalStorageDirectory();
        File directory = new File (sdCard.getAbsolutePath() +
        "/MyFiles");
        File file = new File(directory, "textfile.txt");

        FileInputStream fIn = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fIn);
             char[] inputBuffer = new char[READ_BLOCK_SIZE];
        String s = "";
        int charRead;

    while ((charRead = isr.read(inputBuffer))>0)
    {
        //---convert the chars to a String---
        String readString =
        String.copyValueOf(inputBuffer, 0,
        charRead);
        s += readString;
        inputBuffer = new char[READ_BLOCK_SIZE];
    }

  filename.setText(s);
  filecontent.setText(s);
  Toast.makeText(getBaseContext(),
  "File loaded successfully!",
  Toast.LENGTH_SHORT).show();
  }
  });

its showing all contents of files in each textbox!!i want filename to b extracted from content !!

  • 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-30T14:13:46+00:00Added an answer on May 30, 2026 at 2:13 pm

    Unless you need the file to be human readable, I’d suggest storing the two String values as serialized Java objects using DataInputStream/DataOutputStream:

    Writing:

    public void onClick(View view) {
        String str = filename.getText().toString();
        String str2= filecontent.getText().toString();
    
        File sdCard = Environment.getExternalStorageDirectory();
        File directory = new File (sdCard, "MyFiles");
        directory.mkdirs();
        File file = new File(directory, "textfile.txt");
        FileOutputStream fOut = new FileOutputStream(file);
        DataOutputStream os = new DataOutputStream(fOut);
        os.writeUTF(str);
        os.writeUTF(str2);
        os.close();
    }
    

    Reading:

    public void onClick(View v) {    
        File sdCard = Environment.getExternalStorageDirectory();
        File directory = new File (sdCard, "MyFiles");
        File file = new File(directory, "textfile.txt");
    
        FileInputStream fIn = new FileInputStream(file);
        DataInputStream is = new DataInputStream(fIn);
        String name = is.readUTF();
        String content = is.readUTF();
        is.close();
    
        filename.setText(name);
        filecontent.setText(content);
        Toast.makeText(getBaseContext(),
            "File loaded successfully!",
            Toast.LENGTH_SHORT).show();
    }
    

    Note, however, that it is recommended that all I/O to the SDCard be done in a worker thread. It’s not good practice to do it in an event handler like this. Much better to fire up an AsyncTask for each.

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

Sidebar

Related Questions

I wrote some code to save images on the SD Card a while back.
I currently have my code save text from a two text boxes to a
I have trouble with saving images on sd card. I can see the file
I am using the below code for saving an image to SD Card. But
I have written some code for saving an image to a folder in asp.net.
At the moment my code looks like this: # Assign values for saving to
In my iPhone app I have HomeViewController and ContentViewController. I am saving the values
I use until now this code for saving video that i get from UIImagePickerController
Here what I have on my code I dont see what is wrong on
I was trying recycle code for saving an image and attaching it to an

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.