[Android基础]文件读写操作

来源:互联网 发布:js不等于怎么写 编辑:程序博客网 时间:2024/06/05 08:40

Android系统下的文件,可以分为两类:

一类是共享的文件,如存储在SD卡上的文件,这种文件任何Android应用都可以访问。SD卡的路径在/mnt/sdcard下

另外一类是私有文件,即Android应用自己创建的文件。私有文件的路径在/data/data/应用的包名下


基本操作:

1.创建文件:File.createNewFile()

2.创建文件夹:File.mkdir()

3.在已存在的文件夹下再创建子文件夹:

File file = new File("New" + File.separator + "Sub");//New文件夹已存在

file.mkdir();

4.直接创建多层的文件夹:

File file = new File("New2" + File.separator + "Sub2");//New2文件夹不存在

file.mkdirs();

如果New2文件夹存在,则在New2文件夹的基础上继续创建,处于安全性的考虑,最好都用mkdirs


1.读取assets中的txt文件

try {     //在Android Studio中,assets文件夹要建立在main文件夹下     //即与java、res文件夹同级     InputStream is = getAssets().open("test/Info.txt");     InputStreamReader isr = new InputStreamReader(is, "GBK");     BufferedReader br = new BufferedReader(isr);     String s;     while ((s = br.readLine()) != null) {     Log.i("txt", s);}} catch (IOException e) {     Log.i("txt","wrong");}

2.读取raw中的txt文件(其余同上)

InputStream is = getResources().openRawResource(R.raw.hello);


3.读写内部储存的文件数据

写入:

try {     FileOutputStream fos = openFileOutput("test", Context.MODE_PRIVATE);     OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");     osw.write("nihao啊");     osw.close();     fos.close();     Toast.makeText(getApplication(),"finish",Toast.LENGTH_SHORT).show();} catch (Exception e) {     e.printStackTrace();}


在Android Studio中点击Android Device Monitor,即可打开DDMS.然后选择File Explorer,在data/data目录下可以找到该程序的目录

执行后会发现程序目录下多了一个files文件夹,存放着写入的文件.选中文件后,点击右上方的按钮即可将文件拉到电脑上.


读取:

try {     FileInputStream fis = openFileInput("test");     InputStreamReader isr = new InputStreamReader(fis,"GBK");     char[] a = new char[fis.available()];     isr.read(a);     isr.close();     fis.close();     String s = new String(a);     Log.i("text",s);} catch (Exception e) {     e.printStackTrace();}


4.读写外部储存的文件数据(sd卡,存储路径在mnt/sdcard)

添加权限:

<!-- 在SD卡中创建与删除文件权限--><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/><!-- 往SD卡写入数据权限--><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


写入:

                File sdcard = Environment.getExternalStorageDirectory();                File file = new File(sdcard, "myFile.txt");                if (!sdcard.exists()) {                    Toast.makeText(getApplicationContext(), "当前系统不具备SD卡目录", Toast.LENGTH_SHORT).show();                } else {                    try {                        file.createNewFile();                        Toast.makeText(getApplicationContext(), "文件已经创建完成", Toast.LENGTH_SHORT).show();                        FileOutputStream fos = new FileOutputStream(file);                        OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");                        osw.write("nihao世界啊");                        osw.close();                        fos.close();                        Toast.makeText(getApplicationContext(), "文件已经写入完成", Toast.LENGTH_SHORT).show();                    } catch (IOException e) {                        e.printStackTrace();                    }

读取:

                File sdcard = Environment.getExternalStorageDirectory();                File file = new File(sdcard, "myFile.txt");                if (!sdcard.exists()) {                    Toast.makeText(getApplicationContext(), "当前系统不具备SD卡目录", Toast.LENGTH_SHORT).show();                } else {                    try {                        FileInputStream fis = new FileInputStream(file);                        InputStreamReader isr = new InputStreamReader(fis, "GBK");                        char[] a = new char[fis.available()];                        isr.read(a);                        isr.close();                        fis.close();                        String s = new String(a);                        Log.i("text",s);                    } catch (IOException e) {                        e.printStackTrace();                    }

5.SharedPreferences(存储路径在/data/data/包名/shared_prefs)

只支持boolean int float long String数据类型


写入:

                //SharedPreferences用于读取数据                SharedPreferences sharedPreferences = getPreferences(MainActivity.MODE_PRIVATE);                //Editor用于写入数据                Editor editor = sharedPreferences.edit();                editor.putString("key","value");                if(editor.commit())                    Toast.makeText(getApplicationContext(),"写入成功",Toast.LENGTH_SHORT).show();

读取:

                //SharedPreferences用于读取数据                SharedPreferences sharedPreferences = getPreferences(MainActivity.MODE_PRIVATE);                //Editor用于写入数据                Editor editor = sharedPreferences.edit();                String s = sharedPreferences.getString("key","defaultValue");                Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();


6.SQLite

http://www.cnblogs.com/Excellent/archive/2011/11/19/2254888.html

0 0