<mainactivity.xml>
개정 전
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:onClick="mOnclick"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/load"
android:text="Load"
android:onClick="mOnclick"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/loadres"
android:text="LoadRes"
android:onClick="mOnclick"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/delete"
android:onClick="mOnclick"
android:text="Delete"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edittext"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:onClick="sendMessage"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/load"
android:text="Load"
android:onClick="sendMessage"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/loadres"
android:text="LoadRes"
android:onClick="sendMessage"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/delete"
android:onClick="sendMessage"
android:text="Delete"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edittext"
/>
</LinearLayout>
<mainactivity.java>
개정 전
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.renderscript.ScriptGroup;
import android.view.View;
import android.widget.EditText;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
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)
{
switch(v.getId()) {
case R.id.save:
try {
{
FileOutputStream fos = openFileOutput("test.txt", Context.MODE_PRIVATE);
String str = "Android File IO TEST";
fos.write(str.getBytes());
fos.close();
}
}
catch (Exception e){;}
break;
case R.id.load:
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.loadres:
try {
InputStream fres = getResources().openRawResource(R.raw.restext);
byte[] data = new byte[fres.available()];
while (fres.read(data)!= -1){;}
fres.close();
mEdit.setText(new String(data));
} catch (Exception e){;}
break;
case R.id.delete:
if(deleteFile("test.txt")) {
mEdit.setText("delete Success");
} else {
mEdit.setText("delete Failed");
}
break;
} }
}
개정 후
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.renderscript.ScriptGroup;
import android.view.View;
import android.widget.EditText;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
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 sendMessage(View v)
{
switch(v.getId()) {
case R.id.save:
try {
{
FileOutputStream fos = openFileOutput("test.txt", Context.MODE_PRIVATE);
String str = "Android File IO TEST";
fos.write(str.getBytes());
fos.close();
}
}
catch (Exception e){;}
break;
case R.id.load:
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.loadres:
try {
InputStream fres = getResources().openRawResource(R.raw.restext);
byte[] data = new byte[fres.available()];
while (fres.read(data)!= -1){;}
fres.close();
mEdit.setText(new String(data));
} catch (Exception e){;}
break;
case R.id.delete:
if(deleteFile("test.txt")) {
mEdit.setText("delete Success");
} else {
mEdit.setText("delete Failed");
}
break;
} }
}
res/raw/restext.txt
This file is in resource may be very big
LOAD 시
LoadRes 시
delete 시
save 시 저장되는 경로 Tool windows -> Device file Explorer -> 패키지명 -> files 아래에 저장되어 있다.
참고로 monclick은 deprecation 돼서 더이상 쓰지않는다. 권장 사용법은 아래와 같다
버튼 | Android 개발자 | Android Developers
'프로그래밍 > 안드로이드' 카테고리의 다른 글
리사이클러뷰 - 안드로이드 (0) | 2022.08.22 |
---|---|
다중 이미지 가져오기 - 안드로이드 (0) | 2022.08.08 |
안드로이드 - StartActivityForResult 대체방법 BY Kotlin (0) | 2022.08.06 |
안드로이드 - onclick (0) | 2022.05.17 |
안드로이드 - Preperence (0) | 2022.05.11 |