file io
http://blog.naver.com/PostView.nhn?blogId=hwan2s&logNo=20162242450
ctivity
package com.oks.app;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.dhrod0325.R;
public class AppstudyActivity extends Activity {
private Button btnSave, btnLoad;
private EditText editText;
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSave = (Button) findViewById(R.id.save);
btnSave.setOnClickListener(clickListener);
btnLoad = (Button) findViewById(R.id.load);
btnLoad.setOnClickListener(clickListener);
textView = (TextView)findViewById(R.id.textView1);
editText = (EditText) findViewById(R.id.editText1);
}
View.OnClickListener clickListener = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.save:
try {
FileOutputStream fos = openFileOutput("test.txt",MODE_PRIVATE);
String str = editText.getText().toString();
fos.write(str.getBytes());
fos.close();
Toast.makeText(getApplicationContext(), "writeComplate",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.load:
try {
FileInputStream fis = openFileInput("test.txt");
byte[]data = new byte[fis.available()];
while(fis.read(data)!=-1){;}
fis.close();
textView.setText(new String(data));
} catch (Exception e) {
e.printStackTrace();
}
break;
default:
break;
}
}
};
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/layout" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="android file io test">
<requestFocus />
</EditText>
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save" />
<Button
android:id="@+id/load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="load" />
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
파일을 미리 안드로이드에 포함시켜 놓고 읽기 위한 설정
res/raw 폴더 추가 하고 안에다 파일 위치
그리고 읽음
public String onRawFile(){
String readData;
try {
InputStream fis = getResources().openRawResource(R.raw.rawfile);
byte[] data = new byte[fis.available()];
while(fis.read(data)!=-1){;}
readData = new String(data);
} catch (IOException e) {
readData = "failed read";
e.printStackTrace();
}
return readData;
}
1. asset road resource
1 2 3 4 5 6 7 8 9 10 11 12 | private String getStringAssetFile(Activity activity) throws Exception { AssetManager as = activity.getAssets(); InputStream is = as.open("text1.txt"); // InputStreamReader isr = new // InputStreamReader(as.open("text1.txt"),"EUC-KR"); String text = convertStreamToString(is); is.close(); return text; } |
2. inputStream -> String
1 2 3 4 5 6 7 8 9 10 11 | private String convertStreamToString(InputStream is) throws IOException { ByteArrayOutputStream bs = new ByteArrayOutputStream(); int i = is.read(); while (i != -1) { bs.write(i); i = is.read(); } return bs.toString(); } |
3. raw road resource -> String-> wirte String Builder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | private String getStringFromRawFile(Activity activity) throws IOException { Resources r = activity.getResources(); InputStream is = r.openRawResource(R.raw.text2); InputStreamReader r2 = new InputStreamReader(is); StringBuilder str = new StringBuilder(); ByteArrayOutputStream bs = new ByteArrayOutputStream(); while (true) { int i = r2.read(); if (i == -1) break; else { char c = (char) i; str.append(c); bs.write(c); } } // StringBuilder sb = sb.append(c); // Str ing myText = is.close(); return bs.toString(); } |
////