导航:首页 > 编程语言 > java文件复制

java文件复制

发布时间:2022-02-09 10:25:07

‘壹’ java 把a文件复制到b

复制文件,如图片,音乐文件,建议用字节流.

你没有关闭流.

在my方法的while块后添加下面两行.
br.close();
bw.close();

我写的小例子.
package test;

import java.io.*;

public class FileTest{
public static void main(String[] args){

File from=new File("src\\test\\FileTest.java");
File to=new File("src\\xxxx\\FileTest2.java");
File(from,to);
}
public static void File(File from,File to){
BufferedInputStream in=null;
BufferedOutputStream out=null;
try{
if(!from.exists()){
return;
}
if(!to.exists()){
File toDir=new File(to.getParent());
toDir.mkdirs();
to.createNewFile();
}
in=new BufferedInputStream(new FileInputStream(from));
out=new BufferedOutputStream(new FileOutputStream(to));
int b=0;
while((b=in.read())!=-1){
out.write(b);
}
in.close();
out.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
}

‘贰’ java中如何实现快速复制文件

public class IOTest2 {

/**
* @author jiang
* @param args
* BufferedReader
* BufferedWriter
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//一次能读取一行 readLine()方法
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("1.txt")));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("2.txt")));
String str=null;
while((str=br.readLine())!=null){
//文件末尾读取为null就结束
bw.write(str);
}
bw.flush();//写入后刷新
bw.close();//关闭文件
br.close();//关闭文件
}
}

‘叁’ 怎么用java实现将一个文件的内容复制到另一个文件内容的后面

importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;

publicclassFileCopy{
staticfinalStringfromeFile="c:\test1.txt";
staticfinalStringtoFile="c:\test2.txt";

publicstaticvoidmain(Stringargs[]){
try{
BufferedReaderread=newBufferedReader(newFileReader(newFile(fromeFile)));
FileWriterwrite=newFileWriter(newFile(toFile),true);
Stringtemp;
while((temp=read.readLine())!=null){
write.write(temp);
}
write.close();
read.close();
System.out.println("内容已从"+fromeFile+"复制追加到"+toFile);
}catch(IOExceptione){
e.printStackTrace();
}
}
}

‘肆’ Java文件复制问题

如下修改

修改2

如果满意,望采纳,谢谢!

‘伍’ 使用Java语言如何实现快速文件复制

使用Java语言如何实现快速文件复制:
代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class Test {
public static void main(String[] args){
long start = System.currentTimeMillis();
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
FileChannel inFileChannel = null;
FileChannel outFileChannel = null;
try {
fileInputStream = new FileInputStream(new File("C:\\from\\不是闹着玩的.flv"));
fileOutputStream = new FileOutputStream(new File("C:\\to\\不是闹着玩的.flv"));
inFileChannel = fileInputStream.getChannel();
outFileChannel = fileOutputStream.getChannel();
inFileChannel.transferTo(0, inFileChannel.size(), outFileChannel);//连接两个通道,从in通道读取数据写入out通道。
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fileInputStream != null){
fileInputStream.close();
}
if(inFileChannel != null){
inFileChannel.close();
}
if(fileOutputStream != null){
fileOutputStream.close();
}
if(outFileChannel != null){
outFileChannel.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println("视频文件从“from”文件夹复制到“to”文件需要" + (end - start) + "毫秒。");
}
}

‘陆’ Java怎么实现文件拷贝

工具/原料

一台配置了java环境的电脑

一款适合自己的开发集成环境,这里用的是eclipse Kepler


文件拷贝DEMO

1.首先,理清思路,然后我们再动手操作。

拷贝,有源文件,和目的文件。

如果原文件不存在,提示,报错。

如果目的文件不存在,创建空文件并被覆盖。

如果目的地址,也即目的路径不存在,创建路径。

拷贝,输入流,输出流,关闭流。

拷贝前输出文件大小,计算拷贝大小,比较并核实。输出。

‘柒’ java源文件复制问题

你可能放进去后没有在eclipse相对应的工程中刷新,你也可以先打开eclipse然后再将别人那里的java源文件,放进你的workspace,然后再刷新你就会看到了。如果还是没有,那你就直接复制该文件,然后再粘贴到eclipse的工程目录下就可以了。

‘捌’ 怎样用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());
}
}
}

‘玖’ java 将服务器内的文件复制

你有FTPClient就比较好办,假如你的两台FTP服务器分别为fs1和fs2
在本地开发代码思路如下:
通过FTPClient连接上fs1,然后下载(可以循环批量下载)到本地服务器,保存到一个临时目录。
下载完成后,FTPClient断开与fs1的连接,记得必须logout。
本地服务器通过FileInputStream将刚下载到临时目录的文件读进来,得到一个List<File>集合。
通过FTPClient连接上fs2,循环List<File>集合,将文件上传至fs2的特定目录,然后清空临时目录,上传完毕后,断开fs2的连接,同样必须logout。

‘拾’ java如何实现文件的复制粘贴

打开D盘,点编辑,全部选定,右键点变篮的文件选复制,打开E盘右键点空白处选粘贴。

阅读全文

与java文件复制相关的资料

热点内容
云服务器关机网址不见了 浏览:69
余冠英pdf 浏览:755
开发一个app上市需要什么步骤 浏览:28
phpsleep方法 浏览:430
时间同步服务器ip地址6 浏览:926
钢琴谱pdf下载 浏览:524
香港阿里云服务器怎么封udp 浏览:875
APp买海鲜到哪里 浏览:501
辽油社保app总提示更新怎么办 浏览:586
导入源码教程视频 浏览:613
天翼贷app在哪里下载 浏览:186
app开发源码查看器 浏览:516
程序员发展到了一个瓶颈 浏览:120
程序员去机房干嘛 浏览:697
英雄训练师怎么看曾经服务器 浏览:546
魔兽世界单机输入gm命令 浏览:372
51单片机最大负跳距是多少 浏览:418
android聊天控件 浏览:128
导致压缩机坏的原因 浏览:295
如何多次选取文件夹 浏览:280