public void xie(){
String string = tv.getText().toString().trim();
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/s.txt";
try {
File file = new File(path);
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(string);
bw.flush();
bw.close();
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
上面是往SD卡文件里面写,如果往内部存储包名里面写 可以直接openFileOutput得到输出流。
public void du(){
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/s.txt";
try {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String str = "";
String str1 = "";
while ((str = br.readLine())!= null){
str1+=str;
}
Toast.makeText(this, str1, Toast.LENGTH_SHORT).show();
br.close();
isr.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
以上是读取文件里面的字符串。