txt 파일을 저장하고, 읽어들이고 삭제하는 앱입니다
아래 소스는 통째로 그대로 올렸구요
몇가지 문제가 있더라구요
1) 열기버튼으로 test.txt 식의 파일을 열면
test.txt 로 열립니다 그런데 또 이거를 저장하기 버튼을 누르면
test.txttxt 식으로 저장이되고 이것을 또 열고 저장하면
test.txttxttxt 식으로 무한으로 붙습니다.
열기 할 때 확장자를 제외하고 이름값을 가져오면 이런문제가 안생길것 같은데..
어느부분을 수정해야 할까요? ㅠㅠ
2) 삭제 버튼을 누르면 TEST_TEXT_WRITE 에 저장된 txt파일 리스트가 나열되는데,
단순히 터치한번이면 바로 삭제가됩니다. 터치한번 하면 바로 삭제가 아닌,
정말 삭제하시겠습니까? Yes / no 식으로 창이 뜨고 yes 누르면 삭제 되는것은 어떻게 구현하면 좋을까요?
도움의 말이라도 감사합니다.. ㅠㅠ
* MainActivity.java ( 안드로이드 매니페스트에 write_external_storage 쓰기권한 읽기권한 두개 줬습니다 )
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private String path;//저장하는 곳의 경로
private EditText ed1;//제목 적는 EditText
private EditText ed2;//내용 적는 EditText
private Button bt1;//저장하기 버튼
private Button bt2;//불러오기 버튼
private Button bt3;//삭제하기 버튼
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
path = Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"/TEST_TEXT_WRITE";
ed1 = (EditText)findViewById(R.id.ed1);
ed2 = (EditText)findViewById(R.id.ed2);
bt1 = (Button)findViewById(R.id.bt1);
bt2 = (Button)findViewById(R.id.bt2);
bt3 = (Button)findViewById(R.id.bt3);
bt1.setOnClickListener(btListener);
bt2.setOnClickListener(btListener);
bt3.setOnClickListener(btListener);
}
Button.OnClickListener btListener = new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bt1:
String ed1text = ed1.getText().toString().trim();
if(ed1text.length()>0){
onTextWriting(ed1text,ed2.getText().toString());
}
break;
case R.id.bt2:
onTextRead();
break;
case R.id.bt3:
deltext();
break;
}
}
};
private void onTextWriting(String title,String body){
File file;
file = new File(path);
if(!file.exists()){
file.mkdirs();
}
file = new File(path+File.separator+title+".txt");
try{
FileOutputStream fos = new FileOutputStream(file);
BufferedWriter buw = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));
buw.write(body);
buw.close();
fos.close();
Toast.makeText(this, "저장되었습니다.", Toast.LENGTH_SHORT).show();
}catch(IOException e){
}
}
private void onTextRead(){
final ArrayList<File> filelist = new ArrayList<File>();
File files = new File(path);
if(!files.exists()){
files.mkdirs();
}
if(files.listFiles().length>0){
for(File file : files.listFiles(new TextFileFilter())){
filelist.add(file);
}
}
CharSequence[] filename = new CharSequence[filelist.size()];
for(int i = 0 ; i < filelist.size() ; i++){
filename[i] = filelist.get(i).getName();
}
new AlertDialog.Builder(this)
.setTitle("TEXT FILE LIST")
.setItems(filename, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
try{
String body = "";
StringBuffer bodytext = new StringBuffer();
File selecttext = filelist.get(arg1);
FileInputStream fis = new FileInputStream(selecttext);
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(fis,"UTF-8"));
while((body = bufferReader.readLine())!=null){
bodytext.append(body);
}
ed1.setText(selecttext.getName());
ed2.setText(bodytext.toString());
}catch(IOException e){
}
}
}).setNegativeButton("취소", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
private void deltext(){
final ArrayList<File> filelist = new ArrayList<File>();
File files = new File(path);
if(!files.exists()){
files.mkdirs();
}
if(files.listFiles().length>0){
for(File file : files.listFiles(new TextFileFilter())){
filelist.add(file);
}
}
CharSequence[] filename = new CharSequence[filelist.size()];
for(int i = 0 ; i < filelist.size() ; i++){
filename[i] = filelist.get(i).getName();
}
new AlertDialog.Builder(this)
.setTitle("TEXT FILE LIST")
.setItems(filename, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
filelist.get(arg1).delete();
deltext();
}
}).setNegativeButton("취소", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
class TextFileFilter implements FileFilter{
public boolean accept(File file) {
// TODO Auto-generated method stub
if(file.getName().endsWith(".txt"))return true;
return false;
}
}
}
* activity_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"
>
<EditText
android:id="@+id/ed1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="제목을 적으세요!"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="저장"/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="열기"/>
<Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="삭제"/>
</LinearLayout>
<EditText
android:id="@+id/ed2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="내용을 적으세요!"/>
</LinearLayout>