Android Open Source - CSCI567---Workspace Main Activity






From Project

Back to project page CSCI567---Workspace.

License

The source code is released under:

MIT License

If you think the Android project CSCI567---Workspace listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package csci567.writefile;
//www.  ja  v  a2s .c o m
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;



import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
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 implements OnClickListener {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button buttonwrite = (Button) findViewById(R.id.button1);
    buttonwrite.setOnClickListener(this);
    Button buttonread = (Button) findViewById(R.id.button2);
    buttonread.setOnClickListener(this);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
  
  private boolean writeFile(String string){
    try {
      File file = new File(Environment.getExternalStorageDirectory().getPath() +"/test.txt");
      Log.d("FileWrite: ",Environment.getExternalStorageDirectory().getPath() +"/test.txt");
      // if file doesnt exists, then create it
      if (!file.exists()) {
        file.createNewFile();
      }
 
      FileWriter fw = new FileWriter(file.getAbsoluteFile());
      BufferedWriter bw = new BufferedWriter(fw);
      bw.write(string);
      bw.close();
 
      Log.d("FileWrite: ","Done");
 
    } catch (IOException e) {
      Log.d("FileWriteError: ", e.toString());
    }
    return false;
  }
  
  private String readFile(){
    Log.d("FileRead: ","Starting");
    String toReturn = "";
    try {
      File file = new File(Environment.getExternalStorageDirectory().getPath() +"/test.txt");
 
      // if file doesnt exists, then create it
      if (!file.exists()) {
        Log.d("FileRead: ","No File");
        return "";
      }
 
      FileReader fr = new FileReader(file.getAbsoluteFile());
      BufferedReader br = new BufferedReader(fr); 
      toReturn = br.readLine();
      br.close();
      Log.d("FileRead: ","Done");
 
    } catch (IOException e) {
      Log.d("FileReadError: ", e.toString());
    }
    return toReturn;
  }

  @Override
  public void onClick(View src) {
    switch (src.getId()) {
        case R.id.button1:
          EditText txt = (EditText) findViewById(R.id.edittext); 
            writeFile(txt.getText().toString());
            break;
        case R.id.button2:
          TextView txt2 = (TextView) findViewById(R.id.textView1);
              //Replace the text in the textView with the following text.      
              txt2.setText(readFile());
            break;
    }  
    
  }

}




Java Source Code List

com.example.linearlayoutexample.MainActivity.java
com.example.relativelayoutexample.MainActivity.java
csci567.FragmentExample.Fragmentmanageractivity.java
csci567.FragmentExample.MainActivity.java
csci567.FragmentExample.MyFragment.java
csci567.FragmentExample.MyStaticFragment.java
csci567.alarmexample.MainActivity.java
csci567.alarmexample.SampleAlarmReceiver.java
csci567.asynctaskexample.MainActivity.java
csci567.buttonexample.MainActivity.java
csci567.checkboxexample.MainActivity.java
csci567.doodleexample.MainActivity.java
csci567.doodleexample.SampleAlarmReceiver.java
csci567.doodleexample.SampleBootReceiver.java
csci567.doodleexample.SampleSchedulingService.java
csci567.eventreceiver.DataReceiver.java
csci567.eventreceiver.MainActivity.java
csci567.eventreceiver.RebootReceiver.java
csci567.helloworld.MainActivity.java
csci567.simpledbexample.DBHelper.java
csci567.simpledbexample.MainActivity.java
csci567.suggestionapp.MainActivity.java
csci567.writefile.MainActivity.java
org.ndeftools.boilerplate.AndroidNfcActivity.java
org.ndeftools.boilerplate.DefaultNfcBeamWriterActivity.java
org.ndeftools.boilerplate.DefaultNfcReaderActivity.java
org.ndeftools.boilerplate.DefaultNfcTagWriterActivity.java
org.ndeftools.boilerplate.NdefRecordAdapter.java
org.ndeftools.nfcdemo.TagViewer.java
org.ndeftools.nfcdemo.simulator.FakeTagsActivity.java
org.ndeftools.nfcdemo.simulator.MockNdefMessages.java