我发现此代码似乎是我需要的,因为它会将字节文件复制到SDCard。如何使用此复制代码
但是,如何使用它?说我有一个名为mytext.txt的文本文件,我把它放在我的应用程序中?我将如何参考它?我正在使用Eclipse
public static final void copyfile(String srFile, String dtFile){
try{
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied to " + f2.getAbsolutePath());
} catch(FileNotFoundException ex){
System.out.println(ex.getMessage() + " in the specified directory.");
System.exit(0);
} catch(IOException e){
System.out.println(e.getMessage());
}
}
谢谢你的帮助,如果你不介意的话,我可能需要一点点的手拿着... 我添加了一个类MyUtilities在哪个是你的顶级一套代码和底部的集我把这些代码放到了我的活动中,它似乎并不知道什么是textedit,什么时候引用r.id.text1和text 2 - 它们是什么? 我在哪里可以放上mytext.txt,如何让应用程序知道这是我想复制的文件 - 对不起,我是一个小菜鸟 – MrCloister 2011-04-28 09:10:19
@MrCloister text1和tex2是在XML中定义的“TextEdit”小部件的示例名称文件。我建议你阅读Android开发指南,特别是UI部分:http://developer.android.com/guide/topics/ui/index.html和资源:http://developer.android。com/guide/topics/resources/index.html – Aleadam 2011-04-28 13:02:36
对不起,我正在密集 - 你已经把它们放在那里,所以用户可以指定一个源和目标路径。我将自动复制大量文件,我会看看我是如何与此相关的,并让我们知道我是否取得了成功。 谢谢 – MrCloister 2011-04-28 14:13:21