java如何拷贝文件到另一个目录下
/**
*
复制单个文件
*
@param
oldPath
String
原文件路径
如:c:/fqf.txt
*
@param
newPath
String
复制后路径
如:f:/fqf.txt
*
@return
boolean
*/
public
void
copyFile(String
oldPath,
String
newPath)
{
try
{
int
bytesum
=
0;
int
byteread
=
0;
File
oldfile
=
new
File(oldPath);
if
(oldfile.exists())
{
//文件存在时
InputStream
inStream
=
new
FileInputStream(oldPath);
//读入原文件
FileOutputStream
fs
=
new
FileOutputStream(newPath);
byte[]
buffer
=
new
byte[1444];
int
length;
while
(
(byteread
=
inStream.read(buffer))
!=
-1)
{
bytesum
+=
byteread;
//字节数
文件大小
System.out.println(bytesum);
fs.write(buffer,
0,
byteread);
}
inStream.close();
}
}
catch
(Exception
e)
{
System.out.println(“复制单个文件操作出错”);
e.printStackTrace();
}
}
/**
*
复制整个文件夹内容
*
@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=a.list();
File
temp=null;
for
(int
i
=
0;
i
file.length;
i++)
{
if(oldPath.endsWith(File.separator)){
temp=new
File(oldPath+file[i]);
}
else{
temp=new
File(oldPath+File.separator+file[i]);
}
if(temp.isFile()){
FileInputStream
input
=
new
FileInputStream(temp);
FileOutputStream
output
=
new
FileOutputStream(newPath
+
“/”
+
(temp.getName()).toString());
byte[]
b
=
new
byte[1024
*
5];
int
len;
while
(
(len
=
input.read(b))
!=
-1)
{
output.write(b,
0,
len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory()){//如果是子文件夹
copyFolder(oldPath+”/”+file[i],newPath+”/”+file[i]);
}
}
}
catch
(Exception
e)
{
System.out.println(“复制整个文件夹内容操作出错”);
e.printStackTrace();
}
}
JAVA 把指定的文件 复制到目标文件夹下 怎么写啊?
// 由绝对路径得到输出流
//path源路径
//fileCopeToPath 目标路径
String path=”D:\我的文档\My Pictures\1.jpg “;
String fileCopeToPath=” D:\我的文档\test “;
File file =new File(path);
if(file.exists){
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos= new FileOutputStream( fileCopeToPath+ File.separator +path.subString(path.lastIndexOf(“/\”),path.length));
byte[] b = new byte[fis.available()];
int len = 0;
while ((len = fis.read(b)) != -1)
{
fos.write(b, 0, len);
fos.flush();
}
fos.close();
fis.close();
}
怎样用Java复制文件到指定目录? 在线等,急!!!!
借助工具包commons-io.jar
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class Admin {
public static void main(String[] args) {
File from = new File(“d:/a”);
File to = new File(“d:/b”);
try {
FileUtils.copyDirectory(from, to);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java 将一个文件复制到另一处
test.copy(“G:\\G盘寄存资料\\我的文档1\\音乐课堂.doc”,”G:\\G盘寄存资料”);
请注意上面的有个文件夹名字叫“G盘寄存资料”,你复制的文件后的新文件名也叫“G盘寄存资料”,这样名字重复了,所以就出错了。
可以把程序改成这样的话就行了:
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;
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(ipos)
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[256];
int len=0;
String str=null;
StringBuilder sb=new StringBuilder();
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(“G:\\G盘寄存资料\\我的文档1\\hello.txt”,”G:\\G盘寄存资料”);//++++++++++++++++++++++
}
}
java 复制一个指定文件夹下的指定文件 到另一个指定文件夹下
下面是我学习过程中总结的几个复制文件的方法,代码如下:
/**
* 复制媒体文件,该例子是复制1.mp3文件,列出了四种方式.
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedStreamCopyFiles {
public static void main(String[] args) throws IOException {
/**
* 共有四个方法,但建议用demo1,demo2;因为demo3需要创建数组, 如果文件大,光创建数组就需要很多时间;demo4一定也不要用,
* 效率非常慢.
*/
demo1();
demo2();
demo3();
demo4();
}
public static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream(“d:\\1.mp3”);
FileOutputStream fos = new FileOutputStream(“d:\\01.mp3”);
int len = 0;
byte[] buf = new byte[1024];
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
}
public static void demo2() throws IOException {
FileInputStream fis = new FileInputStream(“d:\\1.mp3”);
BufferedInputStream bufis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(“d:\\02.mp3”);
BufferedOutputStream bufos = new BufferedOutputStream(fos);
int len = 0;
while ((len = bufis.read()) != -1) {
bufos.write(len);
}
bufis.close();
bufos.close();
}
// 不建议这种方式
public static void demo3() throws IOException {
FileInputStream fis = new FileInputStream(“d:\\1.mp3”);
FileOutputStream fos = new FileOutputStream(“d:\\03.mp3”);
byte[] buf = new byte[fis.available()];
fis.read(buf);
fos.write(buf);
fos.close();
fis.close();
}
public static void demo4() throws IOException {
FileInputStream fis = new FileInputStream(“d:\\1.mp3”);
FileOutputStream fos = new FileOutputStream(“d:\\04.mp3”);
int ch = 0;
while ((ch = fis.read()) != -1) {
fos.write(ch);
}
fos.close();
fis.close();
}
}
怎样用Java复制一个文件到指定目录
import java.io.*;
public class CopyFile {
public static void main(String[] args) {
try{
FileInputStream input=new FileInputStream(“f:\\downloads\\kon.jpg”);//可替换为任何路径何和文件名
FileOutputStream output=new FileOutputStream(“f:\\kon.jpg”);//可替换为任何路径何和文件名
int in=input.read();
while(in!=-1){
output.write(in);
in=input.read();
}
}catch (IOException e){
System.out.println(e.toString());
}
}
}