-
文件操作,首先需要一个文件路径(FilePath)
一.在AndroidStudio -> Device File Explorer上查找文件夹 1. Context.getFilesDir().getAbsolutePath()+ File.separator + "user.txt" 对应路径是 /data/user/0/包名/files/user.txt 2. Context.openFileInput("user.txt",MODE_PRIVATE)与Context.openFileOutput() 该方法返回 FileInputStream和FileOutputStream对象 对应路径是 /data/user/0/包名/files/user.txt 3. Context.getCacheDir().getAbsolutePath() + File.separator + "user.txt" 对应路径是 /data/user/0/包名/cache/user.txt 4. Context.getDir("myfile",MODE_PRIVATE).getAbsolutePath() + File.separator + "user.txt" 对应路径是 /data/user/0/包名/app_myfile/user.txt 创建文件的权限 MODE_PRIVATE:只能被当前的应用程序 读写 MODE_APPEND: 其他应用程序也可以向该文件中 追加内容 MODE_WORLD_READABLE:可被其他的应用程序 读取 MODE_WORLD_WRITEABLE:可被其他的应用程序 读写 4. Context.fileList() 返回/data/user/0/包名/files/ 下的所有文件名(String[]) 5. Context.deleteFile(String) 删除/data/user/0/包名/files/ 下指定文件 二.在手机文件管理中 Android 文件夹下找 1. Context.getExternalFilesDir().getAbsolutePath()+ File.separator + "user.txt" 返回 /storage/emulated/0/Android/data/包名/files/myfile/user.txt 2. Context.getExternalCacheDir().getAbsolutePath() + File.separator + "user.txt" 返回 /storage/emulated/0/Android/data/包名/cache/user.txt
-
demo实例
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String userPath = getDir("myfile",MODE_PRIVATE).getAbsolutePath() + File.separator + "user.txt"; Log.e("zjq", "onCreate: " + userPath); findViewById(R.id.btn_save).setOnClickListener(v -> save(userPath)); findViewById(R.id.btn_get).setOnClickListener(v -> get(userPath)); } private void save(String userPath) { new Thread(() -> { User user = new User("zjq"); File file = new File(userPath); ObjectOutputStream objectOutputStream = null; try { // objectOutputStream = new ObjectOutputStream(openFileOutput("myfile.txt",MODE_PRIVATE)); objectOutputStream = new ObjectOutputStream(new FileOutputStream(file)); objectOutputStream.writeObject(user); Log.e("zjq", "save: " + user.getName()); } catch (IOException e) { Log.e("zjq", "save:IOException: " + e.getMessage()); } finally { try { if (objectOutputStream != null) { objectOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } }).start(); } private void get(String userPath) { new Thread(() -> { File file = new File(userPath); if (file.exists()) { ObjectInputStream inputStream = null; try { //inputStream = new ObjectInputStream(openFileInput("myfile.txt")); inputStream = new ObjectInputStream(new FileInputStream(file)); User user = (User) inputStream.readObject(); String name = user.getName(); Log.e("zjq", "get: " + name); runOnUiThread(() -> { TextView textView = findViewById(R.id.tv_name); textView.setText(name); }); } catch (IOException | ClassNotFoundException e) { Log.e("zjq", "get:Exception: " + e.getMessage()); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } }).start(); } }
Andriod 文件(File) 使用
最新推荐文章于 2024-04-27 15:03:52 发布