2017-04-25 47 views
0

我创建了一个应用程序,允许用户创建注释并保存。试图找到内部存储数据的位置的文件路径

我知道数据存储在应用程序的私有存储区域,但我需要通过其文件路径实际查看它存储在其中的文件。任何人都可以协助我如何做到这一点吗?我用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(); 
    } 
} 

}

回答

0

文件变量有一个名为“getAbsolutePath”的方法,这将使你的文件

例如道路,当你将一个变量声明为文件像这样:

File filesDir = context.getFilesDir(); 

然后你就可以用这个方法来得到这样的文件路径:

filesDir.getAbsolutePath(); 
+0

对不起编程不太好,我在哪里准确地将该方法,我需要替换现有的方法或只是添加此行 – Jay1

+0

@ Jay1我编辑我的答案 –

+0

谢谢你,我已经添加了方法,但我想能够在PC上查看用户从文件资源管理器中存储的数据,或者只是找到数据所在的位置以及如何实现,如果这样做有道理的话? – Jay1

0

由于该文件存储在应用程序的内部存储器中,因此无法在设备上物理地看到该文件。 只有您的应用程序可以访问该文件。

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

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

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

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

希望这个答案能帮助你。

+0

感谢您的信息,我对这一切都很陌生,所以现在只是习惯了。还有一种简单的方法可以加密存储的数据吗? – Jay1

+0

是的,有很多方法来加密和解密文件的内容。 - AES-256算法。 - 加密/解密数据的密码原理(公钥,私钥)。 - PGP加密 - (很好的隐私) 如果你不熟悉加密/ PGP算法,寻找一些简单但强大的机制,那么寻找AES-256算法会很好(在互联网上,你会得到AES-256有很多例子)。 在AES算法中,您需要将主密钥保存在安全的地方(您可以将其保存在应用程序内存中或服务器上)。 –

+0

谢谢,我将进一步表示赞赏 – Jay1

相关问题