外部存储到内部存储

外部存储到内部存储

本文介绍了外部存储到内部存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个方案,将在一个EditText保存输入的词SD卡一个文本文件。然而,也有一些是错在我的平板电脑保存的文本文件的内部存储,而不是让即时通讯思想安装SD卡。任何人都可以请帮我如何将此切换到内部存储?任何评论将会大大AP preciated.thank你。

这里是我的code:

 公共无效writeToSDFile(){
    文件根= android.os.Environment.getExternalStorageDirectory();
    tv.append(\\ nExternal文件系统的根:+根);    文件DIR =新的文件(root.getAbsolutePath());
    dir.mkdirs();
    档案文件=新的文件(目录wordlist.txt);    尝试{
        FileOutputStream中F =新的FileOutputStream(文件);        PrintWriter的PW =的新PrintWriter(F);
        pw.println(stringword);
        pw.append(stringword);        pw.flush();
        pw.close();
        f.close();
    }赶上(FileNotFoundException异常五){
        e.printStackTrace();
        Log.i(TAG,未找到*******文件。);
    }赶上(IOException异常五){
        e.printStackTrace();
    }
    tv.append(\\ n \\ n文件写入+文件);
} //结束writeToSDFile


解决方案

由于外部存储设备是可拆卸的,你不应该presume它的存在所有的时间。因此,做任何IO操作之前,检查存储状态。

关于你的问题的内部存储,有两种方法:
1.在应用存储(应用程序缓存) - 是指:Environment.getDataDirectory()
2.在通用的数据目录 - 参考:Context.getCacheDir()

希望这有助于。

I am making a program that would save the inputted word in an EditText to a textfile in sd card. However, there is something wrong about mounting sd card in my tablet so im thinking of saving the text file to internal storage instead. 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.

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().

Hope this helps.

这篇关于外部存储到内部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 17:50