티스토리 뷰
// main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/BtnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
>
</Button>
<Button
android:id="@+id/BtnLoad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load"
>
</Button>
<Button
android:id="@+id/BtnLoadRes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LoadRes"
>
</Button>
<Button
android:id="@+id/BtnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
>
</Button>
<EditText
android:id="@+id/EdtStr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</EditText>
</LinearLayout>
// File.java
package pkg.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class File extends Activity {
/** Called when the activity is first created. */
EditText mEdit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mEdit = (EditText)findViewById(R.id.EdtStr);
findViewById(R.id.BtnDelete).setOnClickListener(mClick);
findViewById(R.id.BtnLoad).setOnClickListener(mClick);
findViewById(R.id.BtnLoadRes).setOnClickListener(mClick);
findViewById(R.id.BtnSave).setOnClickListener(mClick);
}
Button.OnClickListener mClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.BtnSave : try {
FileOutputStream fos = openFileOutput("test.txt", Context.MODE_WORLD_READABLE);
String str = "Android File IO Test";
fos.write(str.getBytes());
fos.close();
mEdit.setText("write success");
} catch(Exception e) {
}
break;
case R.id.BtnLoad : try {
FileInputStream fis = openFileInput("test.txt");
byte[] data = new byte[fis.available()];
while(fis.read(data) != -1) {;}
fis.close();
mEdit.setText(new String(data));
} catch(FileNotFoundException e) {
mEdit.setText("File Not Found");
} catch(Exception e) {
}
break;
case R.id.BtnLoadRes : try {
} catch(Exception e) {
;
}
break;
case R.id.BtnDelete :
if(deleteFile("test.txt")) {
mEdit.setText("delete success");
} else {
mEdit.setText("delete failed");
}
break;
}
}
};
}
'Android > Android Studio' 카테고리의 다른 글
apk 파일 서명 방법 (0) | 2020.01.08 |
---|---|
동그란 Progress (0) | 2019.04.04 |
이미지 버튼 (0) | 2019.04.04 |
내부 스레드 & 핸들러 (0) | 2019.04.04 |
외부 스레드 & 핸들러 (0) | 2019.04.04 |
- Total
- Today
- Yesterday