一、 从 resource 中的 raw 文件夹中获取文件并读取数据(资源文件只能读不能写)
001String res = "";
002
003try{
004
005InputStream in = getResources().openRawResource(R.raw.bbi);
006
007//在\Test\res\raw\bbi.txt,
008
009 intlength = in.available();
010
011 byte [] buffer = newbyte[length];
012
013 in.read(buffer);
014
015 //res = EncodingUtils.getString(buffer, "UTF-8");
016
017 //res = EncodingUtils.getString(buffer, "UNICODE");
018
019 res = EncodingUtils.getString(buffer, "BIG5");
020
021 //依bbi.txt的编码类型选择合适的编码,如果不调整会乱码
022
023 in.close();
024
025 }catch(Exception e){
026
027 e.printStackTrace();
028
029 }
030
031myTextView.setText(res);//把得到的内容显示在TextView上
032
033
034
035二、 从asset中获取文件并读取数据(资源文件只能读不能写)
036
037String fileName = "yan.txt"; //文件名字
038
039String res="";
040
041try{
042
043 InputStream in = getResources().getAssets().open(fileName);
044
045 // \Test\assets\yan.txt这里有这样的文件存在
046
047 intlength = in.available();
048
049byte [] buffer = newbyte[length];
050
051in.read(buffer);
052
053res = EncodingUtils.getString(buffer, "UTF-8");
054
055}catch(Exception e){
056
057 e.printStackTrace();
058
059 }
060
061
062
063三、 从sdcard中去读文件,首先要把文件通过\android-sdk-windows\tools\adb.exe把本地计算机上的文件copy到sdcard上去,adb.exe push e:/Y.txt /sdcard/, 不可以用adb.exe push e:\Y.txt \sdcard\ 同样: 把仿真器上的文件copy到本地计算机上用: adb pull ./data/data/com.tt/files/Test.txt e:/
064
065
066
067String fileName = "/sdcard/Y.txt";
068
069//也可以用String fileName = "mnt/sdcard/Y.txt";
070
071String res="";
072
073try{
074
075FileInputStream fin = new FileInputStream(fileName);
076
077//FileInputStream fin = openFileInput(fileName);
078
079//用这个就不行了,必须用FileInputStream
080
081 intlength = fin.available();
082
083 byte [] buffer = newbyte[length];
084
085 fin.read(buffer);
086
087 res = EncodingUtils.getString(buffer, "UTF-8");
088
089 fin.close();
090
091 }catch(Exception e){
092
093 e.printStackTrace();
094
095}
096
097myTextView.setText(res);
098
099
100
101四、 写文件, 一般写在\data\data\com.test\files\里面,打开DDMS查看file explorer是可以看到仿真器文件存放目录的结构的
102
103 String fileName = "TEST.txt";
104
105 String message = "FFFFFFF11111FFFFF" ;
106
107writeFileData(fileName, message);
108
109
110
111 publicvoidwriteFileData(String fileName,String message){
112
113 try{
114
115 FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);
116
117 byte[] bytes = message.getBytes();
118
119 fout.write(bytes);
120
121 fout.close();
122
123 }
124
125 catch(Exception e){
126
127 e.printStackTrace();
128
129 }
130
131 }
132
133
134
135五、 写, 读data/data/目录(相当AP工作目录)上的文件,用openFileOutput
136
137 //写文件在./data/data/com.tt/files/下面
138
139 publicvoidwriteFileData(String fileName,String message){
140
141 try{
142
143 FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);
144
145 byte[] bytes = message.getBytes();
146
147 fout.write(bytes);
148
149 fout.close();
150
151 }
152
153 catch(Exception e){
154
155 e.printStackTrace();
156
157 }
158
159 }
160
161//-------------------------------------------------------
162
163//读文件在./data/data/com.tt/files/下面
164
165 publicString readFileData(String fileName){
166
167 String res="";
168
169 try{
170
171 FileInputStream fin = openFileInput(fileName);
172
173 intlength = fin.available();
174
175 byte [] buffer = newbyte[length];
176
177 fin.read(buffer);
178
179 res = EncodingUtils.getString(buffer, "UTF-8");
180
181 fin.close();
182
183 }
184
185 catch(Exception e){
186
187 e.printStackTrace();
188
189 }
190
191 returnres;
192
193 }
194
195六、 写, 读sdcard目录上的文件,要用FileOutputStream, 不能用openFileOutput
196
197
198
199 //写在/mnt/sdcard/目录下面的文件
200
201 publicvoidwriteFileSdcard(String fileName,String message){
202
203 try{
204
205 //FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
206
207 FileOutputStream fout = newFileOutputStream(fileName);
208
209 byte[] bytes = message.getBytes();
210
211 fout.write(bytes);
212
213 fout.close();
214
215 }
216
217 catch(Exception e){
218
219 e.printStackTrace();
220
221 }
222
223 }
224
225
226
227 //读在/mnt/sdcard/目录下面的文件
228
229 public String readFileSdcard(String fileName){
230
231 String res="";
232
233 try{
234
235 FileInputStream fin = newFileInputStream(fileName);
236
237 intlength = fin.available();
238
239 byte [] buffer = newbyte[length];
240
241 fin.read(buffer);
242
243 res = EncodingUtils.getString(buffer, "UTF-8");
244
245 fin.close();
246
247 }
248
249 catch(Exception e){
250
251 e.printStackTrace();
252
253 }
254
255 returnres;
256
257 }