0% found this document useful (0 votes)
3 views

Demo Lesson 5

The document contains an Android application code that implements file reading and writing functionalities using internal storage, cache, and external storage. It also includes a login activity that manages user credentials with auto-login features using SharedPreferences. The code defines various methods for handling user interactions and data storage operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Demo Lesson 5

The document contains an Android application code that implements file reading and writing functionalities using internal storage, cache, and external storage. It also includes a login activity that manages user credentials with auto-login features using SharedPreferences. The code defines various methods for handling user interactions and data storage operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

public class MainActivity extends AppCompatActivity implements View.

OnClickListener

findViewById(R.id.btnwriteIS).setOnClickListener(this);
findViewById(R.id.btnreadIS).setOnClickListener(this);
findViewById(R.id.btnwriteFC).setOnClickListener(this);
findViewById(R.id.btnreadFC).setOnClickListener(this);
findViewById(R.id.btnwriteES).setOnClickListener(this);
findViewById(R.id.btnreadES).setOnClickListener(this);
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btnreadIS:
String readDataIS = readIS("myfile.txt");
Toast.makeText(this, "Nôi dung file:" + readDataIS, Toast.LENGTH_SHORT).show();
break;
case R.id.btnwriteIS:
writeIS("myfile.txt","Xin chào các bạn!");
break;
case R.id.btnreadFC:
String readDataFC = readFC("mycached.cache");
Toast.makeText(this, "Data cache: " + readDataFC, Toast.LENGTH_SHORT).show();
break;
case R.id.btnwriteFC:
writeFC("mycached.cache","Dữ liệu lưu vào cahce");
break;
case R.id.btnwriteES:
writeES("mysdcard.txt","Dữ liệu lưu vào thẻ nhớ ngoài");
break;
case R.id.btnreadES:
String readDataEX = readES("mysdcard.txt");
Toast.makeText(this, "Data sdcard: " + readDataEX, Toast.LENGTH_SHORT).show();
break;
}
}
private String readIS(String filename) {
try (FileInputStream fin = openFileInput(filename))
{
byte[] buffer = new byte[1024];
int length = 0;
length = fin.read(buffer);
return new String(buffer,0,length); private void writeIS(String filename,String data) {

} catch ( Exception ex ) { try {


ex.printStackTrace();
} FileOutputStream fout = openFileOutput(filename,MODE_PRIVATE);
fout.write(data.getBytes(StandardCharsets.UTF_8));
return null;
fout.close();
} Toast.makeText(this, "Dữ liệu đã được ghi",
Toast.LENGTH_SHORT).show();
} catch ( Exception ex )
{
Toast.makeText(this, "Error:" + ex.getMessage(),
Toast.LENGTH_SHORT).show();
ex.printStackTrace();
}
}
private void writeFC(String filename, String data) {
File cacheDir = getCacheDir();
try (FileOutputStream fout = new FileOutputStream(new
File(cacheDir,filename))){
fout.write(data.getBytes(StandardCharsets.UTF_8));
Toast.makeText(this, "Dữ liệu lưu vào cache thành
công!", Toast.LENGTH_SHORT).show();

} catch ( Exception ex ) private String readFC(String filename) {


{ File cacheDir = getCacheDir();
ex.printStackTrace(); byte[] buffer = new byte[1024];
Toast.makeText(this, "Error:" + ex.getMessage(), int length = 0;
Toast.LENGTH_SHORT).show(); try (FileInputStream fin= new FileInputStream(new File(cacheDir,
} filename))){
length = fin.read(buffer);
} return new String(buffer,0,length);
} catch ( Exception ex ) {
ex.printStackTrace();
Toast.makeText(this, "Error: " + ex.getMessage(),
Toast.LENGTH_SHORT).show();
}
return null;
}
private void writeES(String filename, String data) {

String sdcard = getExternalFilesDir("") + "/" + filename;

try {
OutputStreamWriter writer = new OutputStreamWriter(new
FileOutputStream(sdcard));
writer.write(data);
writer.close();
Toast.makeText(this, "Lưu thành công", Toast.LENGTH_SHORT).show(); private String readES(String filename) {
} catch ( Exception ex )
{ String sdcard = getExternalFilesDir("") + "/" + filename;
ex.printStackTrace(); try {
}
Scanner scan = new Scanner(new File(sdcard));
}
String data="";
while (scan.hasNext()){
data = data + scan.nextLine() + "\n";
}
scan.close();
return data;
} catch ( Exception ex )
{
ex.printStackTrace();
}
public class LoginActivity extends AppCompatActivity implements View.OnClickListener

findViewById(R.id.btnLogin).setOnClickListener(this);
findViewById(R.id.btnThoat).setOnClickListener(this);
edtUsername = findViewById(R.id.edtUsername);
edtPassword = findViewById(R.id.edtPassword);
ckRememberPassword = findViewById(R.id.ckRememberPassword);

private void saveAutoLogin(){


SharedPreferences preferences = getSharedPreferences("Login", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("username",edtUsername.getText().toString());
editor.putString("password",edtPassword.getText().toString());
editor.commit();
}
private boolean readAutoLogin(){
SharedPreferences preferences = getSharedPreferences("Login", MODE_PRIVATE);
String username = preferences.getString("username","");

if(username != null && !username.equals(""))


{
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
return true;
}
return false;
}

private void clearAutoLogin() {


SharedPreferences preferences = getSharedPreferences("Login", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
}

You might also like