java复制一个文件夹的文本到另外一个文件夹?
CopiesthetextcontentsofafiletoanotherfileSaveitasCopy.java本人新手不会请哪位高手教一下~~~~十分感谢...
Copies the text contents of a file to another file
Save it as Copy.java
本人新手不会 请哪位高手教一下~~~~十分感谢 展开
Save it as Copy.java
本人新手不会 请哪位高手教一下~~~~十分感谢 展开
2个回答
展开全部
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class MyCopy {
public void fileCopy(String sFile, String oFile) {
File file = new File(sFile);
if (!file.exists()) {
System.out.println(sFile + " not have");
return;
}
File fileb = new File(oFile);
if (file.isFile()) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file);
fos = new FileOutputStream(fileb);
byte[] bb = new byte[ (int) file.length()];
fis.read(bb);
fos.write(bb);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else if (file.isDirectory()) {
if (!fileb.exists()) {
fileb.mkdir();
}
String[] fileList;
fileList = file.list();
for (int i = 0; i < fileList.length; i++) {
fileCopy(sFile + "/" + fileList[i], oFile + "/" + fileList[i]);
}
}
}
public static void main(String[] args) {
MyCopy myCopy = new MyCopy();
// myCopy.fileCopy(args[0], args[1]);//dos
myCopy.fileCopy("f:\\qq.txt", "e:\\w.txt");
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class MyCopy {
public void fileCopy(String sFile, String oFile) {
File file = new File(sFile);
if (!file.exists()) {
System.out.println(sFile + " not have");
return;
}
File fileb = new File(oFile);
if (file.isFile()) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file);
fos = new FileOutputStream(fileb);
byte[] bb = new byte[ (int) file.length()];
fis.read(bb);
fos.write(bb);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else if (file.isDirectory()) {
if (!fileb.exists()) {
fileb.mkdir();
}
String[] fileList;
fileList = file.list();
for (int i = 0; i < fileList.length; i++) {
fileCopy(sFile + "/" + fileList[i], oFile + "/" + fileList[i]);
}
}
}
public static void main(String[] args) {
MyCopy myCopy = new MyCopy();
// myCopy.fileCopy(args[0], args[1]);//dos
myCopy.fileCopy("f:\\qq.txt", "e:\\w.txt");
}
}
展开全部
用java复制文件主要是用java的I/O流完成。
代码如下:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
/* 指定源文件的存放路径 */
String str = "C:\\Program Files\\apache-tomcat-7.0.59\\RUNNING.txt";
/* 指定复制后的文件的目标路径 */
String strs = "F:\\迅雷下载\\RUNNING.txt";
/* 创建输入和输出流 */
FileInputStream fis = null;
FileOutputStream fos = null;
try {
/* 将io流和文件关联 */
fis = new FileInputStream(str);
fos = new FileOutputStream(strs);
byte[] buf = new byte[1024 * 1024];
int len;
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询