概述
想要随手写点东西感想之类的,或者一些临时的个人小心思,秘密啥的,又怕联网不安全,写一个纯本地的文件类型的小记事本,无数据库之类的乱七八糟的。也没有什么什么权限要去申请。
先上图

取得本地文件列表
private void getFileList() {
File rootDir = new File(rootPath);
if (!rootDir.exists()) {
//android高版本都要动态申请权限,现在手机基本都是android5之后了
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
}
}
lstFile.clear();
File[] fList = rootDir.listFiles();
if(fList != null && fList.length > 0){
for(File file : fList){
if(file.isDirectory()) continue;
String suffix = file.getName().substring(file.getName().lastIndexOf('.'));
if(suffix.equals(".zgn") || suffix.equals(".txt")){
String filename = file.getName().substring(0, file.getName().lastIndexOf('.'));
lstFile.add(new FileInfoModule(filename, file.getAbsolutePath(), file.lastModified()));
}
}
Collections.sort(lstFile, (fileInfoModule, t1) -> String.valueOf(t1.getLastModified()).compareTo(String.valueOf(fileInfoModule.getLastModified())));
}else{
Pub.showMsgInfo(this, "找不到文件!");
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 100) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
File rootDir = new File(rootPath);
if(!rootDir.mkdirs()) Pub.showMsgInfo(this, "Note目录创建失败!");
}
}
}
点击列表跳转阅读界面
Intent intent = new Intent(this, NoteReadActivity.class);
Bundle bundle = new Bundle();
bundle.putString("filename", lstFile.get(i).getFileName());
bundle.putString("filepath", lstFile.get(i).getFielPath());
bundle.putString("lastmodified", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(lstFile.get(i).getLastModified()));
intent.putExtras(bundle);
startActivity(intent);
根据文件路径读取文件内容
private String getFileContent(String filePath){
if(filePath.equals("")) Pub.showMsgError(this,"文件路径为空!");
try {
File filename = new File(filePath);
FileInputStream fin = new FileInputStream(filename);
BufferedReader reader = new BufferedReader(new InputStreamReader(fin));
String fileContent = "";
String line = "";
while ((line = reader.readLine()) !=null){
fileContent = fileContent + line + "\n";
}
reader.close();
fin.close();
return fileContent;
}catch (Exception e){
Pub.showMsgError(this, e.getMessage());
}
return "";
}
保存文件
private void saveContent() {
//如果标题为空,则生成当前日期的标题
String filename = "";
if (mView.etNoteeditTitle.getText().toString().equals("")) {
filename = new SimpleDateFormat("yyyyMMdd").format(System.currentTimeMillis());
} else filename = mView.etNoteeditTitle.getText().toString();
File file = new File(filePath);
if (file.isDirectory()) {
file = new File(file.getPath(), filename + ".zgn");
if (file.exists()) {
filename = new SimpleDateFormat("yyyyMMdd-HHmmss").format(System.currentTimeMillis());
file = new File(file.getParent(), filename + ".zgn");
}
} else {
String oldfilename = file.getName().substring(0, file.getName().lastIndexOf('.'));
if (!oldfilename.equals(filename)) {
String parentpath = file.getParent();
file.delete();
file = new File(parentpath, filename + ".zgn");
}
}
try {
FileOutputStream fout = new FileOutputStream(file);
fout.write(mView.etNoteeditContent.getText().toString().getBytes());
fout.close();
Intent intent = new Intent(this, NoteReadActivity.class);
Bundle bundle = new Bundle();
bundle.putString("filename", file.getName().substring(0, file.getName().lastIndexOf('.')));
bundle.putString("filepath", file.getAbsolutePath());
bundle.putString("lastmodified", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file.lastModified()));
intent.putExtras(bundle);
startActivityByAnim(intent);
NoteEditActivity.this.finish();
} catch (Exception e) {
Pub.showMsgInfo(this, e.getMessage());
}
}