java复制文本文件代码 java复制文本文件代码怎么写( 二 )


e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
rwFile();
}
}
首先在D盘新建文件1.txt,输入任意内容 。然后执行java代码即可 。
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程序里复制文字import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileCopy {
static final String fromeFile = "c:\\test1.txt";
static final String toFile = "c:\\test2.txt";
public static void main(String args[]) {
try {
BufferedReader read = new BufferedReader(new FileReader(new File(fromeFile)));
FileWriter write = new FileWriter(new File(toFile), true);
String temp;
while((temp = read.readLine())!=null){
write.write(temp);
}
write.close();
read.close();
System.out.println("内容已从"+fromeFile+"复制追加到"+toFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
怎样用java程序实现文件拷贝通过输入输出流解决此问题java复制文本文件代码,具体的可以查看JDK的APIjava复制文本文件代码,实在不会的话 , 百度一下应该都有一堆这方面的代码 。
【java复制文本文件代码 java复制文本文件代码怎么写】关于java复制文本文件代码和java复制文本文件代码怎么写的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

推荐阅读