Android(安卓)- 文件操作 小结
16lz
2021-01-26
在android中的文件放在不同位置,它们的读取方式也有一些不同。
一、资源文件的读取:
1) 从resource的raw中读取文件数据:
01 | String res = "" ; |
02 | try { |
03 |
|
04 |
//得到资源中的Raw数据流 |
05 |
InputStream in = getResources().openRawResource(R.raw.test); |
06 |
07 |
//得到数据的大小 |
08 |
int length = in.available(); |
09 |
10 |
byte [] buffer = new byte [length]; |
11 |
12 |
//读取数据 |
13 |
in.read(buffer); |
14 |
15 |
//依test.txt的编码类型选择合适的编码,如果不调整会乱码 |
16 |
res = EncodingUtils.getString(buffer, "BIG5" ); |
17 |
|
18 |
//关闭 |
19 |
in.close(); |
20 |
21 |
} catch (Exception e){ |
22 |
e.printStackTrace(); |
23 |
} |
2) 从resource的asset中读取文件数据
01 | String fileName = "test.txt" ; //文件名字 |
02 | String res= "" ; |
03 | try { |
04 |
05 |
//得到资源中的asset数据流 |
06 |
InputStream in = getResources().getAssets().open(fileName); |
07 |
08 |
int length = in.available(); |
09 |
byte [] buffer = new byte [length]; |
10 |
11 |
in.read(buffer); |
12 |
13 |
res = EncodingUtils.getString(buffer, "UTF-8" ); |
14 |
15 |
} catch (Exception e){ |
16 |
17 |
e.printStackTrace(); |
18 |
19 |
} |
二、读写/data/data/<应用程序名>目录上的文件:
01 | //写数据 |
02 | public void writeFile(String fileName,String writestr{ |
03 |
try { |
04 |
05 |
FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE); |
06 |
07 |
byte [] bytes = writestr.getBytes(); |
08 |
09 |
fout.write(bytes); |
10 |
11 |
fout.close(); |
12 |
} |
13 |
14 |
catch (Exception e){ |
15 |
e.printStackTrace(); |
16 |
} |
17 | } |
18 |
19 | //读数据 |
20 | public String readFile(String fileName){ |
21 |
String res= "" ; |
22 |
try { |
23 |
FileInputStream fin = openFileInput(fileName); |
24 |
int length = fin.available(); |
25 |
byte [] buffer = new byte [length]; |
26 |
fin.read(buffer); |
27 |
res = EncodingUtils.getString(buffer, "UTF-8" ); |
28 |
fin.close(); |
29 |
} |
30 |
catch (Exception e){ |
31 |
e.printStackTrace(); |
32 |
} |
33 |
return res; |
34 |
35 | } |
三、读写SD卡中的文件。也就是/mnt/sdcard/目录下面的文件 :
01 | //写数据到SD中的文件 |
02 | public void writeFileSdcardFile(String fileName,String write_str){ |
03 |
try { |
04 |
05 |
FileOutputStream fout = new FileOutputStream(fileName); |
06 |
byte [] bytes = write_str.getBytes(); |
07 |
08 |
fout.write(bytes); |
09 |
fout.close(); |
10 |
} |
11 |
12 |
catch (Exception e){ |
13 |
e.printStackTrace(); |
14 |
} |
15 |
} |
16 |
17 |
|
18 | //读SD中的文件 |
19 | public String readFileSdcardFile(String fileName){ |
20 |
String res= "" ; |
21 |
try { |
22 |
FileInputStream fin = new FileInputStream(fileName); |
23 |
24 |
int length = fin.available(); |
25 |
26 |
byte [] buffer = new byte [length]; |
27 |
fin.read(buffer); |
28 |
29 |
res = EncodingUtils.getString(buffer, "UTF-8" ); |
30 |
31 |
fin.close(); |
32 |
} |
33 |
34 |
catch (Exception e){ |
35 |
e.printStackTrace(); |
36 |
} |
37 |
return res; |
38 | } |
四、使用File类进行文件的读写:
01 | //读文件 |
02 | public String readSDFile(String fileName) { |
03 |
04 |
File file = new File(fileName); |
05 |
06 |
FileInputStream fis = new FileInputStream(file); |
07 |
08 |
int length = fis.available(); |
09 |
10 |
byte [] buffer = new byte [length]; |
11 |
fis.read(buffer); |
12 |
13 |
res = EncodingUtils.getString(buffer, "UTF-8" ); |
14 |
15 |
fis.close(); |
16 |
return res; |
17 | } |
18 |
19 | //写文件 |
20 | public void writeSDFile(String fileName, String write_str){ |
21 |
22 |
File file = new File(fileName); |
23 |
24 |
FileOutputStream fos = new FileOutputStream(file); |
25 |
26 |
byte [] bytes = write_str.getBytes(); |
27 |
28 |
fos.write(bytes); |
29 |
30 |
fos.close(); |
31 | } |
五、另外,File还有下面一些常用的操作:
01 | String Name = File.getName(); //获得文件或文件夹的名称: |
02 | String parentPath = File.getParent(); 获得文件或文件夹的父目录 |
03 | String path = File.getAbsoultePath(); //绝对路经 |
04 | String path = File.getPath(); //相对路经 |
05 | File.createNewFile(); //建立文件 |
06 | File.mkDir(); //建立文件夹 |
07 | File.isDirectory(); //判断是文件或文件夹 |
08 | File[] files = File.listFiles(); //列出文件夹下的所有文件和文件夹名 |
09 | File.renameTo(dest); //修改文件夹和文件名 |
10 | File.delete(); //删除文件夹或文件 |
六、使用RandomAccessFile进行文件的读写:
RandomAccessFile的使用方法比较灵活,功能也比较多,可以使用类似seek的方式可以跳转到文件的任意位置,从文件指示器当前位置开始读写。
它有两种构造方法
new RandomAccessFile(f,"rw");//读写方式
new RandomAccessFile(f,"r");//只读方式
使用事例:
查看源码 打印 ?
01 | /* |
02 |
* 程序功能:演示了RandomAccessFile类的操作,同时实现了一个文件复制操作。 |
03 |
*/ |
04 | package com.lwj.demo; |
05 |
|
06 | import java.io.*; |
07 |
|
08 | public class RandomAccessFileDemo { |
09 |
public static void main(String[] args) throws Exception { |
10 |
RandomAccessFile file = new RandomAccessFile( "file" , "rw" ); |
11 |
// 以下向file文件中写数据 |
12 |
file.writeInt( 20 ); // 占4个字节 |
13 |
file.writeDouble( 8.236598 ); // 占8个字节 |
14 |
file.writeUTF( "这是一个UTF字符串" ); // 这个长度写在当前文件指针的前两个字节处,可用readShort()读取 |
15 |
file.writeBoolean( true ); // 占1个字节 |
16 |
file.writeShort( 395 ); // 占2个字节 |
17 |
file.writeLong(2325451l); // 占8个字节 |
18 |
file.writeUTF( "又是一个UTF字符串" ); |
19 |
file.writeFloat( 35 .5f); // 占4个字节 |
20 |
file.writeChar( 'a' ); // 占2个字节 |
21 |
|
22 |
file.seek( 0 ); // 把文件指针位置设置到文件起始处 |
23 |
|
24 |
// 以下从file文件中读数据,要注意文件指针的位置 |
25 |
System.out.println( "——————从file文件指定位置读数据——————" ); |
26 |
System.out.println(file.readInt()); |
27 |
System.out.println(file.readDouble()); |
28 |
System.out.println(file.readUTF()); |
29 |
|
30 |
file.skipBytes( 3 ); // 将文件指针跳过3个字节,本例中即跳过了一个boolean值和short值。 |
31 |
System.out.println(file.readLong()); |
32 |
|
33 |
file.skipBytes(file.readShort()); // 跳过文件中“又是一个UTF字符串”所占字节,注意readShort()方法会移动文件指针,所以不用加2。 |
34 |
System.out.println(file.readFloat()); |
35 |
|
36 |
//以下演示文件复制操作 |
37 |
System.out.println( "——————文件复制(从file到fileCopy)——————" ); |
38 |
file.seek( 0 ); |
39 |
RandomAccessFile fileCopy= new RandomAccessFile( "fileCopy" , "rw" ); |
40 |
int len=( int )file.length(); //取得文件长度(字节数) |
41 |
byte [] b= new byte [len]; |
42 |
file.readFully(b); |
43 |
fileCopy.write(b); |
44 |
System.out.println( "复制完成!" ); |
45 |
} |
46 | } |
总结:
1、apk中有两种资源文件,使用两种不同的方式进行打开使用。
raw使用InputStream in = getResources().openRawResource(R.raw.test);
asset使用InputStream in = getResources().getAssets().open(fileName);
这些数据只能读取,不能写入。
2、SD卡中的文件使用FileInputStream和FileOutputStream进行文件的操作。
3、存放在数据区(/data/data/..)的文件只能使用openFileOutput和openFileInput进行操作。
注意不能使用FileInputStream和FileOutputStream进行文件的操作。
4、RandomAccess类仅限于文件的操作,不能访问其他IO设备。它可以跳转到文件的任意位置,从当前位置开始读写。
转自:http://blog.csdn.net/ztp800201/article/details/7322110
更多相关文章
- Mono for Android(安卓)(4)-- 图片转为二进制,二进制转回图片
- android 系统定制之编译方法总结
- Android(安卓)Studio 使用技巧集锦
- java化改造--xml布局文件转成java——学习整理
- Android(安卓)SDK Manager 和 ADT 更新
- android studio运行时提示All packages are not available for d
- rtl8188eu WiFi移植
- Android(安卓)反编译APK详解
- Android的Apk打包流程