/
复制单个文件
@param
oldPath
String
原文件路径
如:c:/fqftxt
@param
newPath
String
复制后路径
如:f:/fqftxt
@return
boolean
/
public
void
copyFile(String
oldPath,
String
newPath)
{
try
{
int
bytesum
=
0;
int
byteread
=
0;
File
oldfile
=
new
File(oldPath);
if
(oldfileexists())
{
//文件存在时
InputStream
inStream
=
new
FileInputStream(oldPath);
//读入原文件
FileOutputStream
fs
=
new
FileOutputStream(newPath);
byte[]
buffer
=
new
byte[1444];
int
length;
while
(
(byteread
=
inStreamread(buffer))
!=
-1)
{
bytesum
+=
byteread;
//字节数
文件大小
Systemoutprintln(bytesum);
fswrite(buffer,
0,
byteread);
}
inStreamclose();
}
}
catch
(Exception
e)
{
Systemoutprintln("复制单个文件 *** 作出错");
eprintStackTrace();
}
}
/
复制整个文件夹内容
@param
oldPath
String
原文件路径
如:c:/fqf
@param
newPath
String
复制后路径
如:f:/fqf/ff
@return
boolean
/
public
void
copyFolder(String
oldPath,
String
newPath)
{
try
{
(new
File(newPath))mkdirs();
//如果文件夹不存在
则建立新文件夹
File
a=new
File(oldPath);
String[]
file=alist();
File
temp=null;
for
(int
i
=
0;
i
<
filelength;
i++)
{
if(oldPathendsWith(Fileseparator)){
temp=new
File(oldPath+file[i]);
}
else{
temp=new
File(oldPath+Fileseparator+file[i]);
}
if(tempisFile()){
FileInputStream
input
=
new
FileInputStream(temp);
FileOutputStream
output
=
new
FileOutputStream(newPath
+
"/"
+
(tempgetName())toString());
byte[]
b
=
new
byte[1024
5];
int
len;
while
(
(len
=
inputread(b))
!=
-1)
{
outputwrite(b,
0,
len);
}
outputflush();
outputclose();
inputclose();
}
if(tempisDirectory()){//如果是子文件夹
copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
}
}
}
catch
(Exception
e)
{
Systemoutprintln("复制整个文件夹内容 *** 作出错");
eprintStackTrace();
}
}
你好:
请看代码:
/把一个文件夹里的所有文件包括文件夹 一并原样拷贝到另一个目录中;
@author shuishui
/
import javaioFile;
import javaioFileInputStream;
import javaioFileNotFoundException;
import javaioFileOutputStream;
import javaioIOException;
import javaioInputStream;
import javaioOutputStream;
public class CopyDir001 {
public static File dirFrom;
public static File dirTo;
// 目标路径创建文件夹
public void listFileInDir(File file) {
File[] files = filelistFiles();
for (File f : files) {
String tempfrom = fgetAbsolutePath();
String tempto = tempfromreplace(dirFromgetAbsolutePath(),
dirTogetAbsolutePath()); // 后面的路径 替换前面的路径名
if (fisDirectory()) {
File tempFile = new File(tempto);
tempFilemkdirs();
listFileInDir(f);
} else {
Systemoutprintln("源文件:" + fgetAbsolutePath());
//
int endindex = temptolastIndexOf("\\");// 找到"/"所在的位置
String mkdirPath = temptosubstring(0, endindex);
File tempFile = new File(mkdirPath);
tempFilemkdirs();// 创建立文件夹
Systemoutprintln("目标点:" + tempto);
copy(tempfrom, tempto);
}
}
}
/
封装好的文件拷贝方法
/
public void copy(String from, String to) {
try {
InputStream in = new FileInputStream(from);
OutputStream out = new FileOutputStream(to);
byte[] buff = new byte[1024];
int len = 0;
while ((len = inread(buff)) != -1) {
outwrite(buff, 0, len);
}
inclose();
outclose();
} catch (FileNotFoundException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
}
}
public static void main(String[] args) {
File fromfile = new File("e:\\shui\\test");// 源文件夹
File tofile = new File("e:\\Jying\\shui");// 目标
CopyDir001 copy = new CopyDir001();
// 设置来源去向
copydirFrom = fromfile;
copydirTo = tofile;
copylistFileInDir(fromfile);
}
}
import javaio;
public class FileCopyDemo{
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new FileReader("D:\\1txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\copytxt"));
String line = "";
while((line=brreadLine())!=null){
bwwrite(line);
bwnewLine();
bwflush();
}
bwclose();
brclose();
}
}
File fileOld = new File(pathOld);
File fileNew = new File(pathNew);
if(fileOldexists()){
try {
FileInputStream fis = new FileInputStream(fileOld);
FileOutputStream fos = new FileOutputStream(fileNew);
int read = 0;
while ((read = fisread()) != -1) {
foswrite(read);
fosflush();
}
fosclose();
fisclose();
} catch (FileNotFoundException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
}
package chapter06sy6_3;
import javaio;
class test3_demo
{
public static void main(String[]args)
{
//定义BufferedReader处理流的引用
BufferedReader br=null;
//定义BufferedWriter处理流的引用
BufferedWriter bw=null;
//定义BufferedWriter处理流的引用
BufferedWriter out=null;
try
{
//将指定的FileReader节点流封装成处理流
br=new BufferedReader(new FileReader("sy6_3java"));
//将指定的FileWriter节点流封装成处理流
bw=new BufferedWriter(new FileWriter("Testtxt"));
//将BufferedWriter处理流再封装成处理流
out=new PrintWriter(bw);
}
catch(IOException e){
eprintStackTrace();
}
//将读取的一行内容写入指定的文件
outprintln(s);
//将读取的一行内容打印在控制台
Systemoutpritln(s);
}
}
以上就是关于java如何拷贝文件到另一个目录下全部的内容,包括:java如何拷贝文件到另一个目录下、java如何拷贝一个文件夹内的多个指定的文件到另外一个指定的文件夹下、2. 用Java编写一个程序,使用字符流拷贝一个文本文件等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)