Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (10.2k points)

I want to save a file to the internal storage by getting the text inputted from EditText. Then I want the same file to return the inputted text in String form and save it to another String which is to be used later.

Here's the code:

package com.omm.easybalancerecharge;

import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.telephony.TelephonyManager;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

public class MainActivity extends Activity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        final EditText num = (EditText) findViewById(R.id.sNum);

        Button ch = (Button) findViewById(R.id.rButton);

        TelephonyManager operator = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

        String opname = operator.getNetworkOperatorName();

        TextView status = (TextView) findViewById(R.id.setStatus);

        final EditText ID = (EditText) findViewById(R.id.IQID);

        Button save = (Button) findViewById(R.id.sButton);

        final String myID = ""; //When Reading The File Back, I Need To Store It In This String For Later Use

        save.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                //Get Text From EditText "ID" And Save It To Internal Memory

            }

        });

        if (opname.contentEquals("zain SA")) {

            status.setText("Your Network Is: " + opname);

        } else {

            status.setText("No Network");

        }

        ch.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                //Read From The Saved File Here And Append It To String "myID"

                String hash = Uri.encode("#");

                Intent intent = new Intent(Intent.ACTION_CALL);

                intent.setData(Uri.parse("tel:*141*" + /*Use The String With Data Retrieved Here*/ num.getText()

                        + hash));

                startActivity(intent);

            }

        });

    }

I have included comments to help you further analyze my points as to where I want the operations to be done/variables to be used.

1 Answer

0 votes
by (46k points)

Try:

public static void writeStringAsFile(final String fileContents, String fileName) {

    Context context = App.instance.getApplicationContext();

    try {

        FileWriter out = new FileWriter(new File(context.getFilesDir(), fileName));

        out.write(fileContents);

        out.close();

    } catch (IOException e) {

        Logger.logError(TAG, e);

    }

}

public static String readFileAsString(String fileName) {

    Context context = App.instance.getApplicationContext();

    StringBuilder stringBuilder = new StringBuilder();

    String line;

    BufferedReader in = null;

    try {

        in = new BufferedReader(new FileReader(new File(context.getFilesDir(), fileName)));

        while ((line = in.readLine()) != null) stringBuilder.append(line);

    } catch (FileNotFoundException e) {

        Logger.logError(TAG, e);

    } catch (IOException e) {

        Logger.logError(TAG, e);

    } 

    return stringBuilder.toString();

}

Related questions

0 votes
1 answer
asked Jul 26, 2019 in Java by Shubham (3.9k points)
0 votes
1 answer
asked Sep 15, 2019 in Java by Suresh (3.4k points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...