StringBuilder content =new StringBuilder();
try {
File eFile =new File(getFilePath(this,"Test")+File.separator+"SSS.text");
if (!eFile.exists())
eFile.createNewFile();
FileOutputStream outputStream =new FileOutputStream(eFile);
String str ="hello word" +"\n" +"android"
outputStream.write(str.getBytes());
outputStream.close();
InputStreamReader bufferedInputStream =new InputStreamReader(new FileInputStream(eFile));
BufferedReader buffreader =new BufferedReader(bufferedInputStream);
String line;
while ((line = buffreader.readLine()) !=null) {//一行一行读取
content.append(line).append("\n");
}
Log.e("afterInCreate", content.toString());
bufferedInputStream.close();
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
public static StringgetFilePath(Context context, String dir) {
String directoryPath="";
if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ) {//判断外部存储是否可用
directoryPath =context.getExternalFilesDir(dir).getAbsolutePath();
}else{//没外部存储就使用内部存储
directoryPath=context.getFilesDir()+File.separator+dir;
}
File file =new File(directoryPath);
if(!file.exists()){//判断文件目录是否存在
file.mkdirs();
}
return directoryPath;
}
网友评论