import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class FileCopy {
public void copy(String src, String dest) {
InputStream is = null;
OutputStream os = null;
char ch[] = src.toCharArray();
// ************新添加的代码,获取文件名**********
int pos = 0;
for (int i = ch.length - 1; i >= 0; i--) {
if (ch[i] == '\\') {
if (i > pos)
pos = i;
}
}
String temp = src.substring(pos);
dest = dest + temp;
System.out.println("dest=" + dest);
try {
is = new BufferedInputStream(new FileInputStream(src));
os = new BufferedOutputStream(new FileOutputStream(dest));
byte[] b = new byte[1024];
int len = 0;
String str = null;
try {
while ((len = is.read(b)) != -1) {
os.write(b, 0, len);
}
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
FileCopy test = new FileCopy();
test.copy("F:\\mirror\\api.postalmethods.com\\PostalWSWSDL.asmx", "D:\\WSDL");
}
}
将一个文件拷贝到另一个文件夹中
最新推荐文章于 2021-04-13 16:09:18 发布
本文提供了一个使用Java进行文件复制的示例代码。通过使用BufferedInputStream和BufferedOutputStream,该示例展示了如何从源文件读取数据并将其写入目标文件。此外,还包含了获取文件名并将其附加到目标路径的功能。
1313

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



