java - 使用文件输入/输出流方法安全存储数据

标签 java android fileinputstream fileoutputstream

如果我使用文件输入/输出流方法,即使是基本安全性,存储在应用程序中的数据是否安全,例如是否会使未经授权的访问该数据变得更加困难?这是数据存储的类。

public class Utilities {

public static final String FILE_EXTENSION = ".bin";

public static boolean saveNote(Context context, Notes notes){
    String fileName = String.valueOf(notes.getDateTime()) + FILE_EXTENSION;

    FileOutputStream fos;
    ObjectOutputStream oos;

    try {

        fos = context.openFileOutput(fileName, context.MODE_PRIVATE);
        oos = new ObjectOutputStream(fos);
        oos.writeObject(notes);
        oos.close();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
        return false; //tell the user something went wrong
    }

    return true;
}

public static ArrayList<Notes> getSavedNotes(Context context) {
    ArrayList<Notes> notes = new ArrayList<>();

    File filesDir = context.getFilesDir();
    filesDir.getAbsolutePath();
    ArrayList<String> noteFiles = new ArrayList<>();

    for(String file : filesDir.list()) {
        if(file.endsWith(FILE_EXTENSION)) {
            noteFiles.add(file);
        }
    }

    FileInputStream fis;
    ObjectInputStream ois;

    for(int i = 0; i < noteFiles.size(); i++) {
        try{
            fis = context.openFileInput(noteFiles.get(i));
            ois = new ObjectInputStream(fis);

            notes.add((Notes)ois.readObject());

            fis.close();
            ois.close();



        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            return null;

        }
    }

    return notes;

}

public static Notes getNoteByName(Context context, String fileName) {
    File file = new File(context.getFilesDir(), fileName);
    Notes notes;

    if(file.exists()) {
        FileInputStream fis;
        ObjectInputStream ois;

        try {
            fis = context.openFileInput(fileName);
            ois = new ObjectInputStream(fis);

            notes = (Notes) ois.readObject();

            fis.close();
            ois.close();

        } catch(IOException | ClassNotFoundException e){
            e.printStackTrace();
            return null;
        }

        return notes;
    }

    return null;
}

public static void deleteNote(Context context, String fileName) {
    File Dir = context.getFilesDir();
    File file = new File(Dir, fileName);

    if (file.exists()) file.delete();

}

}

最佳答案

是的,但它与FileInputStream没有直接关系。您碰巧正在写入 internal storage 的应用程序部分。唯一有权访问这些文件的是您的应用程序和获得 root 权限的设备用户。如果您在其他地方写作 - 例如 external storage - 更多应用程序可能可以访问它。

关于java - 使用文件输入/输出流方法安全存储数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43710317/

相关文章:

java - 如何在 JBoss 7 中配置 PeriodicSizeRotatingFileHandler?

java - 从 MySql 表生成 DAO 的 Pojo 和 JSP 的工具/Eclipse 插件?

java - 在 ScrolledComposite 中调整扩展栏 SWT 的大小

android - 在一个对象中同时使用 jackson 和 ormlite 注释时出现异常

java - 如何比较 Hashmap 中的 2 个数组列表?

android - 位图和 BufferedImages 之间的区别

java - 如何使用 FileInputStream 加载配置 xml 文件,但出现 FileNotFoundException

java - 制作文件输入流时未找到文件异常?

java fileinputstream可用方法返回始终为零

java - 如何使用我的应用程序 (Java) 在 Android 中设置桌面图案(背景)?