简体   繁体   English

外部存储到内部存储

[英]External storage to internal storage

I am making a program that would save the inputted word in an EditText to a textfile in sd card. 我正在制作一个程序,将EditText中输入的单词保存到SD卡中的文本文件中。 However, there is something wrong about mounting sd card in my tablet so im thinking of saving the text file to internal storage instead. 但是,在我的平板电脑上安装SD卡有一些问题,所以我想把文本文件保存到内部存储器。 Can anyone please help me how to switch this to internal storage? 任何人都可以帮我如何切换到内部存储? any comment would be greatly appreciated.thank you. 任何评论都会非常感谢。谢谢。

Here's my code: 这是我的代码:

public void writeToSDFile() {


    File root = android.os.Environment.getExternalStorageDirectory(); 
    tv.append("\nExternal file system root: "+root);    

    File dir = new File (root.getAbsolutePath());
    dir.mkdirs();
    File file = new File(dir, "wordlist.txt");

    try {   


        FileOutputStream f = new FileOutputStream(file);           

        PrintWriter pw = new PrintWriter(f);
        pw.println(stringword);
        pw.append(stringword);        

        pw.flush();
        pw.close();
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.i(TAG, "******* File not found.");
    } catch (IOException e) {
        e.printStackTrace();
    }   
    tv.append("\n\nFile written to "+file);    


}//end writeToSDFile

Since external storage is removable, you should not presume that it exists all the time. 由于外部存储是可移动的,因此您不应该假定它一直存在。 Hence before doing any io operation, check the storage state. 因此,在执行任何io操作之前,请检查存储状态。

About your question to internal storage, There are two ways: 1. In application storage (app cache) - refer: Environment.getDataDirectory() 2. In common Data directory - refer: Context.getCacheDir(). 关于你对内部存储的问题,有两种方法:1。在应用程序存储(app cache)中 - 参考:Environment.getDataDirectory()2。在公共数据目录中 - 参考:Context.getCacheDir()。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM