1. 程式人生 > >安卓文件存儲(一)

安卓文件存儲(一)

tostring rate 存儲 gen ide import ins troy ace

package com.example.sql_file_Input;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import com.example.sql_file.R;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.EditText;

public class MainActivity extends Activity {

private EditText edit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = findViewById(R.id.edit);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
String inputText=edit.getText().toString();
save(inputText);
}
private void save(String inputText) {
FileOutputStream out=null;
BufferedWriter write=null;
try {
out=openFileOutput("data.txt",Context.MODE_PRIVATE);
write=new BufferedWriter(new OutputStreamWriter(out));
write.write(inputText);
if (write != null) {
write.close();//文件編寫完必須關閉
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

安卓文件存儲(一)