簡體   English   中英

使用文件輸入/輸出流方法存儲數據安全

[英]is storing data using file input/output stream method secure

如果我使用文件輸入/輸出流方法,即使它是基本安全性,存儲在應用程序中的數據是否也是安全的,例如它是否會使未經授權訪問該數據變得更加困難? 這是數據存儲的類。

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無直接關系。 您恰好正在寫入應用程序的內部存儲部分。 訪問這些文件的唯一內容是您的應用和root用戶設備。 如果你在其他地方寫作 - 比如外部存儲 - 更多應用可能有權訪問它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM