我已经从服务器复制数据的任务。我使用BufferedInputStream
和输出流来复制数据,我正在逐字节地做。即使它正在运行,但它需要很长时间才能复制数据,因为其中一些数据的大小为100 MB,所以绝对不会奏效。任何人都可以建议我任何字节副本的副本,以便我的代码可以复制几百MB的文件。 缓冲区是2048在Java中复制数据的更快捷方式?
下面是我的代码如下所示:
static void copyFiles(SmbFile[] files, String parent) throws IOException {
SmbFileInputStream input = null;
FileOutputStream output = null;
BufferedInputStream buf_input = null;
try {
for (SmbFile f : files) {
System.out.println("Working on files :" + f.getName());
if (f.isDirectory()) {
File folderToBeCreated = new File(parent+f.getName());
if (!folderToBeCreated.exists()) {
folderToBeCreated.mkdir();
System.out.println("Folder name " + parent
+ f.getName() + "has been created");
} else {
System.out.println("exists");
}
copyFiles(f.listFiles(), parent + f.getName());
} else {
input = (SmbFileInputStream) f.getInputStream();
buf_input = new BufferedInputStream(input, BUFFER);
File t = new File(parent + f.getName());
if (!t.exists()) {
t.createNewFile();
}
output = new FileOutputStream(t);
int c;
int count;
byte data[] = new byte[BUFFER];
while ((count = buf_input.read(data, 0, BUFFER)) != -1) {
output.write(data, 0, count);
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (input != null) {
input.close();
}
if (output != null) {
output.close();
}
}
}
分享你的代码。 – vels4j
现在你已经显示了代码,除非BUFFER是1,否则你不会逐字节地读取数据。(你的异常处理可以做一些工作,顺便说一下......这几乎从来都不是合适的catch块。 ) –
@JonSkeet你的意思是我应该把尽量捕捉每一个文件,这样即使一个文件失败,其余应该不会受到影响 – Dude