java批量复制文件

fengzhe0411 2009-09-03 07:52:47
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
*
* @author Administrator
*/
public class IOTest {
public static void main(String[] args){
try{
FileInputStream fis=new FileInputStream("a.txt");
FileOutputStream[] fos=new FileOutputStream[100];
for(int i=0;i<fos.length;i++){
fos[i]=new FileOutputStream("a"+i+".txt");
int read=fis.read();
while(read!=-1){
fos[i].write(read);
read=fis.read();
}

fos[i].close();
}
fis.close();
}
catch(IOException e){
e.printStackTrace();
}
}

}


问题在于fis.read()移到-1后怎么移回去....
...全文
461 9 打赏 收藏 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
@井九 2009-09-03
  • 打赏
  • 举报
回复
感觉直接exec系统命令可能还简单点
程序员猪佩琪 2009-09-03
  • 打赏
  • 举报
回复
感觉这种效率很低:
public static void main(String[] args){
try{
FileInputStream fis=new FileInputStream("a.txt");
FileOutputStream[] fos=new FileOutputStream[100];
for(int i=0;i <fos.length;i++){
fos[i]=new FileOutputStream("a"+i+".txt");
int read=fis.read();
while(read!=-1){
fos[i].write(read);
read=fis.read();
}
fis.close();
fos[i].close();
fis = new FileInputStream("a"+i+".txt");
System.out.println(1);
}

}
catch(IOException e){
e.printStackTrace();
}
}
feishare 2009-09-03
  • 打赏
  • 举报
回复
可以加个if语句进行判断,如果等于-1的话在读取第二个文件
  • 打赏
  • 举报
回复
可以用apache common的FileUtils
bunrise 2009-09-03
  • 打赏
  • 举报
回复
/**
* 复制目录下单个文件
*
* @param oldPass
* @param newPath
* @return
*/
public Boolean copyFile(String oldPath, String newPath) {
try {
File file = new File(oldPath);
if (file.isDirectory()) {
return false;
}

if (file.exists()) {
FileInputStream inputStream = new FileInputStream(file);
FileOutputStream outputStream = new FileOutputStream(newPath);
BufferedInputStream reader = new BufferedInputStream(
inputStream);
int len = inputStream.available();
byte[] buffer = new byte[len];

reader.read(buffer, 0, len);
outputStream.write(buffer);

inputStream.close();
outputStream.close();

reader.close();

}
} catch (Exception e) {
System.out.println("copy file failed!");
e.printStackTrace();
}
return true;
}

/**
* 复制目录下所有文件和目录
*
* @param oldPath
* @param newPath
*/
@SuppressWarnings("static-access")
public Boolean copyAllFile(String oldPath, String newPath) {

File file = new File(oldPath);

if (!file.exists()) {
return false;
}

if (file.isFile()) {
copyFile(oldPath, newPath);
return false;
}

File[] fileList = file.listFiles();
File mkdir = new File(newPath);

if (!mkdir.exists()) {
mkdir.mkdirs();
}

for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isDirectory()) {
copyAllFile(oldPath + fileList[i].separator
+ fileList[i].getName(), newPath
+ fileList[i].separator + fileList[i].getName());
} else {
copyFile(oldPath + fileList[i].separator
+ fileList[i].getName(), newPath
+ fileList[i].separator + fileList[i].getName());
}
}

return true;

}

自己写的,不敢保证效率
hxbot 2009-09-03
  • 打赏
  • 举报
回复
对于流的关闭方法直接抛出了异常,交给jre运行时系统处理,这样并不好!。



import java.io.*;

public class IOTest {
public static void main(String[] args) throws IOException{

File f = new File("C:/Users/hxbot/Desktop/a.txt");
if (f.exists()) {
Reader fis = null;
BufferedReader bfr = null;

Writer write = null;
BufferedWriter bfw = null;

String temp = "";
for(int i=0; i<100 ; i++){
fis = new FileReader(f);
bfr = new BufferedReader(fis);

write = new FileWriter(new File("C:/Users/hxbot/Desktop/a" + (i+1) + ".txt"));
bfw = new BufferedWriter(write);

while( (temp = bfr.readLine()) != null ){
bfw.write(temp);
bfw.newLine();
}
bfw.flush();
}

bfw.close();
write.close();

bfr.close();
fis.close();

} else {
System.out.println("源(src)文件不存在,请先创建。");
}
}
}

hxbot 2009-09-03
  • 打赏
  • 举报
回复
楼主的意思是:假设硬盘上有一个名为 “a.txt” 的文本文件

以这个"a.txt"为源(src)文件复制一百遍。形成a.txt a1.txt a2.txt a3.txt a4.txt **** a100.txt

这样理解有错吗?
uastation 2009-09-03
  • 打赏
  • 举报
回复
看起来不太明白,是复制文件呢?还是复制文件里的内容?看楼主代码的内容是在复制内容..
fengzhe0411 2009-09-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 liuwenjie517333813 的回复:]
感觉这种效率很低:
              public static void main(String[] args){
        try{
        FileInputStream fis=new FileInputStream("a.txt");
        FileOutputStream[] fos=new FileOutputStream[100];
        for(int i=0;i <fos.length;i++){
            fos[i]=new FileOutputStream("a"+i+".txt");
            int read=fis.read();
            while(read!=-1){
                fos[i].write(read);
                read=fis.read();
            }
            fis.close();
            fos[i].close();
            fis = new FileInputStream("a"+i+".txt");
            System.out.println(1);
        }
       
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
[/Quote]


这个是复制一个 再用新的去复制
不能把原来的复制100遍吗

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧