java文件复制到指定位置

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文件复制到指定位置

怎样用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());

}

}

}

本文来自投稿,不代表【】观点,发布者:【

本文地址: ,如若转载,请注明出处!

举报投诉邮箱:253000106@qq.com

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2024年3月25日 09:19:33
下一篇 2024年3月25日 09:27:02

相关推荐

  • 深入java虚拟机pdf,深入java虚拟机 中村成洋 pdf

    在linux环境下,java怎么实现从word格式转换为pdf格式 //设置当前使用的打印机,我的Adobe Distiller打印机名字为 Adobe PDF wordCom.setProperty( ActivePrinter , new Variant( Adobe PDF ));//设置printout的参数,将word文档打印为postscript…

    2024年5月23日
    3800
  • java截取指定长度字符串,java截取指定字符串之后的

    java中如何截取字符串中的指定一部分 第一个参数是开始截取的字符位置。(从0开始)第二个参数是结束字符的位置+1。(从0开始)indexof函数的作用是查找该字符串中的某个字的位置,并且返回。 int end);截取s中从begin开始至end结束时的字符串,并将其赋值给s;split讲解:java.lang.string.split split 方法 将…

    2024年5月23日
    3600
  • java绑定一个端口,java使用端口

    java如何多个service共用一个端口 你如果有多个项目的话,你可以把多个项目放到一个tomcat里面,这样端口相同使用项目名称来进行区分项目。你如果非要使用同一个,你也可以配置不同的域名导向不同的项目。就是访问的域名不同转接到的项目不同。 如果需要同时启动多个程序,要么修改tomcat的配置文件中的监听端口。要么修改jar包程序的监听端口。不能在一台服…

    2024年5月23日
    2900
  • linux给文件写权限,linux怎么给文件权限

    linux宝塔写入权限不足 1、可以使用chmod命令修改权限。语法:chmod [-cfvR] [–help] [–version] mode file…说明 : Linux/Unix 的档案调用权限分为三级 : 档案拥有者、群组、其他。利用 chmod 可以藉以控制档案如何被他人所调用。 2、通过更改文件权限可以使用c…

    2024年5月23日
    4400
  • java多线程并发编程基础,Java多线程并发执行返回

    电脑培训分享Java并发编程:核心理论 电脑培训发现本系列会从线程间协调的方式(wait、notify、notifyAll)、Synchronized及Volatile的本质入手,详细解释JDK为我们提供的每种并发工具和底层实现机制。 人们开始意识到了继承的众多缺点,开始努力用聚合代替继承。软件工程解决扩展性的重要原则就是抽象描述,直接使用的工具就是接口。接…

    2024年5月23日
    4100
  • 自学java找工作,自学java找工作需要包装简历吗

    自学java学多久可以找到工作 1、自学Java至少需要一年以上的时间才能达到找工作的水平。报班培训四到六个月的时间就可以找到一份不错的工作。 2、自学Java至少需要一年以上的时间才能达到找工作的水平。 3、如果要想找到一份Java相关的工作,需要至少学习5-6个月时间才能就业。Java开发需要掌握一些基础的编程语言知识,比如掌握面向对象的编程思想、基本的…

    2024年5月23日
    3800
  • 包含c语言对txt文件命名的词条

    如何在C语言编程里面修改源文件名字 如果你是在WINDOWS的话,简单了,随便用个编辑器,比如记事本,然后写c源程序,保存到你想要保存的位置。如果你在DOS下,可以用edit,写好以后,按alt键,选择文件菜单,然后保存。 用open打开文件,注意操作模式使用“修改”或者“添加” 用write或者fprintf向文件中写入你的内容。 用close关闭文件。 …

    2024年5月23日
    4300
  • java左移右移,java 左移

    java位移问题 1、思路:直接用Integer类的bit运算操作。 2、移位操作:左移:向左移位,符号后面的数字是移了多少位,移的位用0补齐,例如2进制数01111111左移一位后变为11111110,移位是字节操作。 3、Java 位运算 Java 位运算[转]一,Java 位运算表示方法: 在Java语言中,二进制数使用补码表示,最高位为符号位,正数的…

    2024年5月23日
    3700
  • java获取字符串指定字符,java获取字符串指定字符的位置

    java如何读取字符串中的某一段字符串 1、在Java中获取字符串中指定的值可以使用以下几种方式:点击学习大厂名师精品课使用substring()方法可以使用String类提供的substring(intbeginIndex,intendIndex)方法,从原始字符串中截取出指定范围的字符子串。 2、第一个参数是开始截取的字符位置。(从0开始)第二个参数是结…

    2024年5月23日
    3500
  • java技术规范,java规范性要求

    现在主流的JAVA技术是什么? java最流行开发技术程序员必看 1 、Git Git一直是世界上最受欢迎的Java工具之一,也是Java开发人员最杰出的工具之一。Git是一个开源工具,是-种出色的分布式版本控制解决方案。 (1).Java基础语法、数组、类与对象、继承与多态、异常、范型、集合、流与文件、反射、枚举、自动装箱和注解。(2).Java面向对象编…

    2024年5月23日
    3400

发表回复

登录后才能评论



关注微信