package com.ty.innerstore_demo1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//openFileInput返回一个指向默认内部存储位置的FileInputStream
//openFileOutput同理
public class MainActivity extends AppCompatActivity {
String innerPath = null;
Button btn_Save;
Button btn_Show;
EditText et_Input;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_Save = (Button) findViewById(R.id.BTN_Save);
btn_Show = (Button) findViewById(R.id.BTN_Show);
et_Input = (EditText) findViewById(R.id.ET_Input);
}
public void OnClick(View v) {
switch (v.getId()) {
case R.id.BTN_Save:
SaveFile();
break;
case R.id.BTN_Show:
LoadFile();
break;
}
}
private void LoadFile() {
FileInputStream fis = null;
int count = 0;
byte[] b = new byte[5];
try {
fis = openFileInput("Inner.txt");
while ((count = fis.read(b))!=-1)
{
Toast.makeText(this, new String(b,0,count), Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fis != null)
{
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void SaveFile() {
FileOutputStream fos = null;
try {
fos = openFileOutput("Inner.txt", MODE_APPEND);
fos.write(et_Input.getText().toString().getBytes());
fos.flush();
Toast.makeText(this, "Saved Text", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*自定义内部存储目录的读写
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
innerPath = getFilesDir().getPath() + "/MyDir";
File file = new File(innerPath);
if (!file.exists()) {
file.mkdir();
}
btn_Save = (Button) findViewById(R.id.BTN_Save);
btn_Show = (Button) findViewById(R.id.BTN_Show);
et_Input = (EditText) findViewById(R.id.ET_Input);
}
public void OnClick(View v) {
FileOutputStream fos = null;
FileInputStream fis = null;
File filePath = new File(innerPath, "Toast.txt");
switch (v.getId()) {
case R.id.BTN_Save:
try {
fos = new FileOutputStream(filePath, true);
fos.write(et_Input.getText().toString().getBytes());
fos.flush();
Toast.makeText(this, "Saved Text", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
break;
case R.id.BTN_Show:
byte[] b = new byte[1024];
int count = 0;
try {
fis = new FileInputStream(filePath);
while ((count = fis.read(b))!= -1)
{
Toast.makeText(this, new String(b,0,count), Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
*/
}
内部存储案例
最新推荐文章于 2022-04-13 22:58:07 发布