在过去的几天里,我一直在尝试将文件从usb闪存驱动器复制到我的nexus 7内存。有没有办法传输文件而不必指定文件名?到目前为止,这是我的代码。
public void copyFile(File usb, File dst) throws IOException
{
//checks to see if the backup directory is created
if(dst.isDirectory())
{
//displays message saying directory is created
mvfldr.show();
//if else statement checks to see if pen drive drive is connected
if(!usb.exists())
{
//displays message if pen drive is not connected
chkffd2.show();
}
else
{
//displays pen drive is connected to the device
chkffd.show();
String[] children = usb.list();
for(int i=0; i<children.length; i++)
{
copyFile(new File(usb, children[i]), new File(dst, children[i]));
}
}
}
//directory is not created, and create the directory.
else
{
//displaying message saying folder has not been created, and will be created.
bldng.show();
dst.mkdir();
if(!usb.exists())
{
//displaying flash drive has not been connected
chkffd2.show();
}
else
{
//displaying flash drive had been connected.
chkffd.show();
String[] children = usb.list();
for(int i=0; i<children.length; i++)
{
copyFile(new File(usb, children[i]), new File(dst, children[i]));
}
}
}
InputStream in = new FileInputStream(usb);
OutputStream out = new FileOutputStream(dst);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0);
{
out.write(buf, 0, len);
}
in.close();
out.close();
}
});
}
}