Skip to content

Instantly share code, notes, and snippets.

@edenizk
Last active May 11, 2018 17:22
Show Gist options
  • Save edenizk/d78603ba5b8ff06cdea55cccada8e766 to your computer and use it in GitHub Desktop.
Save edenizk/d78603ba5b8ff06cdea55cccada8e766 to your computer and use it in GitHub Desktop.
read write
package com.example.deniz.readwritefile;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class MainActivity extends AppCompatActivity {
String msg = "0121";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText edtText = (EditText) findViewById(R.id.edtText);
final TextView txtRead = (TextView) findViewById(R.id.txtread);
final TextView blntext = (TextView) findViewById(R.id.blntext);
Button btnRead = (Button) findViewById(R.id.btnRead);
Button btnWrite = (Button) findViewById(R.id.btnWrite);
Button btn0 = (Button) findViewById(R.id.btn0);
btnWrite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String msg = edtText.getText().toString();
String saveFile = "save_file";
try {
FileOutputStream outputSaveFile = openFileOutput(saveFile,MODE_PRIVATE);
outputSaveFile.write(msg.getBytes());
outputSaveFile.close();
Toast.makeText(getApplicationContext(),"Saved",Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
btnRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
int i=0;
FileInputStream saveFile = openFileInput("save_file");
InputStreamReader inputReader = new InputStreamReader(saveFile);
BufferedReader buffReader = new BufferedReader(inputReader);
StringBuffer strBuffer = new StringBuffer();
// msg=buffReader.readLine();
// Log.i("LOG", msg);
char a = buffReader.readLine().charAt(0);
txtRead.setText(strBuffer.toString());
if(a=='1') {
blntext.setText("denz");
}
else{
blntext.setText("asdasd");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// txtRead.setText(msg.charAt(0));
}
});
btn0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
char a = msg.charAt(0);
if(a=='1') {
blntext.setText("denz");
}
else{
blntext.setText("asdasd");
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment