AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
MainActivity.java:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "data";
private String filename = "text.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = findViewById(R.id.btn1);
Button btn2 = findViewById(R.id.btn2);
Button btn3 = findViewById(R.id.btn3);
Button btn4 = findViewById(R.id.btn4);
Button btn5 = findViewById(R.id.btn5);
Button btn6 = findViewById(R.id.btn6);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
}
@Override
public void onClick(View view) {
File sdcard = Environment.getExternalStorageDirectory();
File myfile = new File(sdcard, "this my file.txt");
String i;
switch (view.getId()) {
case R.id.btn1:
try {
InputStream is = getResources().getAssets().open("text.txt");
InputStreamReader isr = new InputStreamReader(is,"UTF-8");
BufferedReader br = new BufferedReader(isr);
while ((i = br.readLine()) != null){
Log.i(TAG,i);
}
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.btn2:
try {
InputStream is = getResources().openRawResource(R.raw.text);
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
while ((i = br.readLine()) != null) {
Log.i(TAG,i);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.btn3:
try {
FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
EditText et = findViewById(R.id.et);
osw.write(et.getText().toString());
osw.flush();
fos.flush();
osw.close();
fos.close();
Toast.makeText(this, "写入完成", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.btn4:
try {
FileInputStream fis = openFileInput(filename);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
char input[] = new char[fis.available()];
isr.read(input);
isr.close();
fis.close();
String readed = new String(input);
Toast.makeText(this, ""+readed, Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.btn5:
if (!sdcard.exists()) {
Toast.makeText(this, "当前系统不具备sd卡目录", Toast.LENGTH_SHORT).show();
}
System.out.println(sdcard.getAbsolutePath());
try {
myfile.createNewFile();
Toast.makeText(this, "文件创建完成", Toast.LENGTH_SHORT).show();
FileOutputStream fos = new FileOutputStream(myfile);
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
EditText et2 = findViewById(R.id.et2);
osw.write(et2.getText().toString());
osw.flush();
fos.flush();
osw.close();
fos.close();
Toast.makeText(this, "文件写入完成", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.btn6:
if (myfile.exists()) {
try {
FileInputStream fis = new FileInputStream(myfile);
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
char input[] = new char[fis.available()];
isr.read(input);
isr.close();
fis.close();
String str = new String(input);
Toast.makeText(this, ""+str, Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
}