java中怎样按字节读取文件并复制到另一个文件夹

文件得按字节读取,一边可以读取任何类型文件,实现的功能:复制该文件并放到某个文件夹中
首先感谢所有帮忙的人,由于进展还有以下要求:
1.按字节读取和写文件,这样可以应用于任何文件
2.还有要复制文件到2个文件夹中,比如:复制文件夹 copy 下所有文件到 文件夹 paste1 和 文件夹 paste2 中,要求查找paste1中有发过去的文件时,才把 copy 下相应的文件删除, paste2 作为复制文件的备份文件夹(保存所有已发文件,其结果应该和 paste1 中文件相同)
3.若复制的文件不是独享的(比如此文件是打开状态的:此时文件有可能没有处理完全就复制就会产生错误),则此文件即使复制过去了,也要删掉,等文件独享了再重新复制
4.读取时最好能将文件的名称和路径也写到新文件中,但要定义 得到的文件 没有附加信息(名称和路径),比如:读取时把附加信息读取再复制, 并定义好 只保留 正文信息,以便于传输

此问题关闭,到期后也不给分,补充问题已经重新发布.
http://zhidao.baidu.com/question/95008153.html

这里以字节流FileInputStream,FileOutputStream为例。代码例子如下:

import java.io.File;
/**
 * 把一个文件夹中的文件复制到一个指定的文件夹
 * @author young
 *
 */
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFile {
public static void main(String[] args) {
/* 指定源exe文件的存放路径 */
String str = "f:/jdk-1_5_0_06-windows-i586-p.exe";
/* 指定复制后的exe的目标路径 */
String strs = "e:/copy.exe";
/* 创建输入和输出流 */
FileInputStream fis = null;
FileOutputStream fos = null;

try {
/* 将io流和文件关联 */
fis = new FileInputStream(str);

fos = new FileOutputStream(strs);
byte[] buf = new byte[1024 * 1024];
int len;
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);

}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-04-23
楼上的差不多了,只是不是按字节读取
把bufferedReader改成fileinputStream就好了~
第2个回答  2009-04-22
import java.io.*;
public class CopyExample10_7 {
public static void main(String[] args)throws IOException {
FileReader fr = new FileReader("1.txt");
int ch = 0;
while((ch = fr.read())!=-1 )
{
System.out.println(ch);
}
int b = 0;
try {
FileReader in = new FileReader("1.txt");
FileWriter out = new FileWriter("newstudent.txt");
while((b=in.read())!=-1){
out.write(b);
}
out.close();
in.close();

} catch (FileNotFoundException e2) {
System.out.println("找不到指定文件");
System.exit(-1);
} catch (IOException e1) {
System.out.println("文件复制错误");
System.exit(-1);
}
}
}
第3个回答  推荐于2017-11-27
import java.io.*;
public class ReadWriteFile{
public static void main(String [] args){
try{
File read = new File("c:\\test1.txt");
File write = new File("c:\\test2.txt");
BufferedReader br = new BufferedReader(new FileReadder(read));
BufferedWriter bw = new BufferedWriter(new FileWriter(write));
String temp =null;
temp=br.readLine();
while(temp!=null){
//写文件,只适用与windows系统
bw.write(temp+"\r\n");
//继续写文件
temp = br.readLine();

}
bw.close();
br.close();
}catch(FileNotFoundException fe){
System.out.println(fe.getMessage());
fe.printStackTrace();
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}

private void copyFile2(String source, String dest) {
try {
File in = new File(source);
File out = new File(dest);
FileInputStream inFile = new FileInputStream(in);
FileOutputStream outFile = new FileOutputStream(out);
byte[] buffer = new byte[1024];
int i = 0;
while ((i = inFile.read(buffer)) != -1) {
outFile.write(buffer, 0, i);
}//end while
inFile.close();
outFile.close();
}//end try
catch (Exception e) {

}//end catch
}//end copyFile本回答被提问者采纳
第4个回答  2009-04-23
不对。。不能用FileReader
要用FileInputStream,和FileOutputStream
相似回答