外部ストレージに保存する場合はmanifest fileへの記載が必要みたいだけど、内部ストレージなら不要のようだ
1. activity_main.xml (前回の使いまわし)
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText_dataText"
android:layout_weight="1.04"
android:text="good morning" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="@+id/button_save"
android:onClick="onButtonSaveClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load"
android:id="@+id/button_load"
android:onClick="onButtonLoadClick" />
</LinearLayout>
<!-- 読み込み用 -->
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/labelList"
/>
</LinearLayout>
2. MainActivity.java
package com.example.training.filelocaloperation;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class MainActivity extends Activity {
TextView tv;
String LOCAL_FILE = "test.text";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.editText_dataText);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onButtonSaveClick(View v){
String text = tv.getText().toString();
Toast.makeText(MainActivity.this, new StringBuilder().append("Saveクリックした").append(text).toString(), Toast.LENGTH_SHORT).show();
OutputStream out;
try {
// out = openFileOutput(LOCAL_FILE,MODE_APPEND); // 追記
out = openFileOutput(LOCAL_FILE, Context.MODE_PRIVATE); // 上書き
PrintWriter writer = new PrintWriter(new OutputStreamWriter(out,"UTF-8"));
writer.append(text);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
// tv.setText("Saveクリック");
}
public void onButtonLoadClick(View v){
Toast.makeText(MainActivity.this, "Loadクリックした", Toast.LENGTH_SHORT).show();
// tv.setText("Loadクリック");
StringBuilder text = new StringBuilder();
InputStream inputStream;
String lineBuffer = null;
try {
inputStream = openFileInput(LOCAL_FILE);
BufferedReader reader= new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
while( (lineBuffer = reader.readLine()) != null ){
text.append(lineBuffer);
Log.d("FileAccess", lineBuffer);
}
} catch (IOException e) {
e.printStackTrace();
}
TextView lblList = (TextView)this.findViewById(R.id.labelList);
lblList.setText(text);
}
}
0 件のコメント:
コメントを投稿