책과 여기저기 안드로이드 파일 입출력에 관한 자료를 뒤졌는데 모두 잘 안되어 헤메던 중 아래 Happy Programmer분의 글에 주어진 예제가 잘 동작하여 파일 입력과 읽기에 적용해서 성공했습니다.
https://bitsoul.tistory.com/tag/안드로이드 저장장치 내부저장장치 InternalStorage openFileInput openFileOutput [Happy Programmer~]
===================================================
main_activity.java
===================================================
package com.jstudio.myfile;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class MainActivity extends AppCompatActivity {
EditText mEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEdit = (EditText)findViewById(R.id.edittext);
}
public void mOnclick(View v){
FileOutputStream fos=null;
BufferedOutputStream bos=null;
DataOutputStream dos = null;
switch (v.getId()){
case R.id.save:
try {
String str = "Android File IO test";
fos = openFileOutput("text.txt",Context.MODE_APPEND);
PrintWriter out = new PrintWriter(fos);
out.println(str);
out.close();
mEdit.setText("write test");
}catch (FileNotFoundException fnfe) {
mEdit.setText("File Not Found 2");
return;
} catch ( Exception e){
mEdit.setText("error");
}
break;
case R.id.load:
try{
StringBuffer data1 = new StringBuffer();
FileInputStream fis = openFileInput("text.txt");//파일명
BufferedReader buffer = new BufferedReader(new InputStreamReader(fis));
//BufferedReader buffer = new InputStreamReader(fis);
String str = buffer.readLine(); // 파일에서 한줄을 읽어옴
while (str != null) {
data1.append(str + "\n");
str = buffer.readLine();
}
mEdit.setText(data1);
buffer.close();
mEdit.setText(new String(data));*/
} catch (Exception e){
mEdit.setText("File Not Found");
}
break;
case R.id.loadres:
try{
InputStream MyInput = getResources().openRawResource(R.raw.test);
byte[] data = new byte[MyInput.available()];
while (MyInput.read(data) !=-1){;}
MyInput.close();
mEdit.setText(new String(data));
} catch (Exception e){;}
break;
case R.id.delete:
if (deleteFile("text.txt")) {
mEdit.setText("delete success");
} else {
mEdit.setText("delete Failed");
}
break;
}}
}
====================================================
activity_main.xml
====================================================
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="mOnclick"
android:text="save"/>
<Button
android:id="@+id/load"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="mOnclick"
android:text="load"/>
<Button
android:id="@+id/loadres"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="mOnclick"
android:text="loadres"/>
<Button
android:id="@+id/delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="mOnclick"
android:text="delete"/>
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</androidx.appcompat.widget.LinearLayoutCompat>
'IT 통신 수학 과학 > 자바 안드로이드' 카테고리의 다른 글
Response Status Code (0) | 2021.11.02 |
---|---|
안드로이드 kotlin view binding (0) | 2021.10.18 |
[Android/Kotlin] DataBinding(데이터바인딩) (0) | 2021.03.20 |
Android Studio : WebView 구현 오류 문제. (0) | 2021.02.22 |
AdapterView 다중선택 (0) | 2020.11.02 |
AdapterView2 (0) | 2020.11.01 |
ArrayAdapter (0) | 2020.10.28 |
Android popmenu (0) | 2020.09.08 |