输入流输出流的根本作用是为了永久性的数据存储,这里利用输入流将raw文件夹下的.js文件读到buffer缓冲区中,再写到手机指定路径下(即一个文件)。(File更应该叫做一个路径)
例如EngineNew项目中:
copyBinary(R.raw.widget6510, path + "widget6510.js");
public void copyBinary(int id, String path) {
// log("copy -> " + path);
try {
InputStream ins = getResources().openRawResource(id);
int size = ins.available();
// Read the entire resource into a local byte buffer.
byte[] buffer = new byte[size];
ins.read(buffer); // read()一次读取一个字节
ins.close();
FileOutputStream fos = new FileOutputStream(path);
fos.write(buffer);
fos.close();
} catch (Exception e) {
Log.d("public void createBinary() error! : " , e.getMessage().toString());
}
}
在AutoMgrNew 项目中: 原理上都是控制输入流和输出流,理论一样,细节略有不同下面使用的是文件输入输出流
public int getFileFromServer(String downLoadUrl, String pathUrl, String name) {
this.isSotp = false;
if(Environment.getExternalStorageState().equals("mounted")) {
try {
try {
URL e = new URL(downLoadUrl);
HttpURLConnection conn = (HttpURLConnection)e.openConnection();
conn.setConnectTimeout(5000);
long allSize = (long)conn.getContentLength();
this.downLoadListener.start(allSize, name);
InputStream is = conn.getInputStream();
File file = new File(pathUrl);
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[1024];
int total = 0;
byte var15;
do {
int len;
if((len = bis.read(buffer)) == -1) {
fos.close();
bis.close();
is.close();
this.downLoadListener.stop();
var15 = 1;
return var15;
}
fos.write(buffer, 0, len);
total += len;
this.downLoadListener.progressSize((long)total, allSize);
} while(!this.isSotp);
this.downLoadListener.stop();
var15 = 0;
return var15;
} catch (MalformedURLException var21) {
var21.printStackTrace();
} catch (FileNotFoundException var22) {
var22.printStackTrace();
} catch (IOException var23) {
var23.printStackTrace();
}
return 0;
} finally {
;
}
} else {
this.downLoadListener.stop();
return 0;
}
}
本文介绍了一种从资源文件夹复制文件到设备指定路径的方法,并详细展示了如何通过HTTP请求从服务器下载文件到本地存储的过程。
5802

被折叠的 条评论
为什么被折叠?



