【问题标题】:trying to locate file path for where data is stored internally试图找到内部存储数据的文件路径
【发布时间】:2017-04-25 17:34:08
【问题描述】:

我创建了一个应用程序,允许用户创建笔记并保存它。

我知道数据存储在应用程序的私有存储区域,但我需要通过文件路径实际查看存储数据的文件。谁能帮助我如何做到这一点?我使用了 FileOutputStream 和 FileInputStream 方法

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();
    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();
    }
}

}

【问题讨论】:

    标签: android location filepath


    【解决方案1】:

    文件变量有一个名为“getAbsolutePath”的方法,它将为您提供文件的路径

    例如,当您像这样将变量声明为 File 时:

    File filesDir = context.getFilesDir();
    

    那么你可以使用这个方法来获取文件路径如下:

    filesDir.getAbsolutePath();
    

    【讨论】:

    • 对不起,编程不太好,我该把那个方法放在哪里,我需要替换现有方法还是只添加这一行
    • 谢谢,我已经添加了方法,但我希望能够查看用户从 PC 上的文件资源管理器中存储的数据,或者只是找到数据所在的位置以及如何到达那里,如果这有意义吗?
    【解决方案2】:

    您将无法在设备上实际看到该文件,因为该文件存储在应用程序的内部存储器中。 只有您的应用程序可以访问该文件。

    要可视化/探索该文件,您需要根设备。 Android Studio 具有文件资源管理器的功能, 工具菜单 > Android > Android 设备监视器 但您将能够看到公用文件夹结构。

    马哈茂德提供的答案是正确的。 要检索文件路径,您可以使用以下函数。

    File filesDir   = context.getFilesDir();
    String filePath = filesDir.getAbsolutePath();
    

    所有文件都将存储在该位置。

    希望这个答案对你有所帮助。

    【讨论】:

    • 感谢您的信息,我对所有这些都是新手,所以现在只是习惯了。还有一种简单的方法可以加密存储的数据吗?
    • 是的,有很多方法可以加密和解密文件的内容。 - AES-256 算法。 - 加密/解密数据的密码学原理(公钥、私钥)。 - PGP 加密 - (Pretty Good Privacy) 如果您不熟悉 Cryptography/PGP 算法并寻找一些简单但强大的机制,那么寻找 AES-256 算法会很好(在互联网上,您会得到很多示例使用 AES-256)。在 AES 算法中,您需要将主密钥保存在安全的地方(您可以将其保存在应用程序内存或服务器上)。
    • 谢谢,我会进一步调查,非常感谢
    猜你喜欢
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    相关资源
    最近更新 更多