展会信息港展会大全

Android读写文件
来源:互联网   发布日期:2016-01-13 22:01:05   浏览:2318次  

导读:一、从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写)001String res =;002003try{004005InputStream in = getResources().openRawResource(R.raw.bbi);006007//在\Test\res\raw\bbi......

一、从 resource 中的 raw 文件夹中获取文件并读取数据(资源文件只能读不能写)

001

String res = "";

002

003

try{

004

005

InputStream in = getResources().openRawResource(R.raw.bbi);

006

007

//在\Test\res\raw\bbi.txt,

008

009

int length = in.available();

010

011

byte [] buffer = new byte[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

031

myTextView.setText(res);//把得到的内容显示在TextView上

032

033

034

035

二、从asset中获取文件并读取数据(资源文件只能读不能写)

036

037

String fileName = "yan.txt"; //文件名字

038

039

String res="";

040

041

try{

042

043

InputStream in = getResources().getAssets().open(fileName);

044

045

// \Test\assets\yan.txt这里有这样的文件存在

046

047

int length = in.available();

048

049

byte [] buffer = new byte[length];

050

051

in.read(buffer);

052

053

res = 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

067

String fileName = "/sdcard/Y.txt";

068

069

//也可以用String fileName = "mnt/sdcard/Y.txt";

070

071

String res="";

072

073

try{

074

075

FileInputStream fin = new FileInputStream(fileName);

076

077

//FileInputStream fin = openFileInput(fileName);

078

079

//用这个就不行了,必须用FileInputStream

080

081

int length = fin.available();

082

083

byte [] buffer = new byte[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

097

myTextView.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

107

writeFileData(fileName, message);

108

109

110

111

public voidwriteFileData(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

public voidwriteFileData(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

public String readFileData(String fileName){

166

167

String res="";

168

169

try{

170

171

FileInputStream fin = openFileInput(fileName);

172

173

int length = fin.available();

174

175

byte [] buffer = new byte[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

return res;

192

193

}

194

195

六、写, 读sdcard目录上的文件,要用FileOutputStream, 不能用openFileOutput

196

197

198

199

//写在/mnt/sdcard/目录下面的文件

200

201

public voidwriteFileSdcard(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 = new FileInputStream(fileName);

236

237

int length = fin.available();

238

239

byte [] buffer = new byte[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

return res;

256

257

}

注: openFileOutput是在raw里编译过的,FileOutputStream是任何文件都可以

赞助本站

人工智能实验室

相关热词: 读写 文件 读取 写入

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港