批量网文件夹中写入文件
批量网文件夹中写入文件,文件夹下可以是文件夹列表,批量写入java执行

批量网文件夹中写入文件
package com.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
* 批量网文件夹中写入文件
*
* @author 原创 http://www.javakcsj.com/
* @date2018-8-19
*/
public class WriteFileBatch {
public static void main(String[] args) {
WriteFileBatch.done();
}
public static void done() {
File addOne = new File("E:\\xxxx.txt");// 要添加的文件
File addtwo = new File("E:\\xxxx.doc");
File file = new File("E:\\dir");// 需要批量写入到这儿的文件夹,里面可以是文件夹
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
continue;
}
copyFile(addOne.getPath(),
files[i].getPath() + "\\" + addOne.getName());
copyFile(addtwo.getPath(),
files[i].getPath() + "\\" + addtwo.getName());
}
}
/**
* 复制单个文件
*
* @param oldPath
* String 原文件路径 如:c:/fqf.txt
* @param newPath
* String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public static void copyFile(String oldPath, String newPath) {
System.out.println(oldPath);
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];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; // 字节数 文件大小
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
} else {
System.out.println("不存在源路径");
}
} catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
/**
* 复制整个文件夹内容
*
* @param oldPath
* String 原文件路径 如:c:/fqf
* @param newPath
* String 复制后路径 如:f:/fqf/ff
* @return boolean
*/
public static 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批量压缩文件

利用zip工具,批量将文件夹或者文件压缩,提供java源代码下载(java课程设计网原创)...查看更多
java图片切割

java课程设计之java图片切割,提供java源代码下载(java课程设计网原创)...查看更多
批量给自己项目下的java文件添加注释

批量给自己项目下的java文件添加注释...查看更多
基于Misty1算法的加密软件(JAVA)的实现

java课程设计之基于Misty1算法的加密软件(JAVA)的实现,提供java源代码下载(java课程设计网原创)...查看更多
发送邮件客户端,带发送附件

java课程设计之发送邮件客户端,带发送附件,提供java源代码下载(java课程设计网原创)...查看更多
java经纬度算两点之间的距离

java课程设计之java经纬度算两点之间的距离,提供java源代码下载(java课程设计网原创)...查看更多
批量网文件夹中写入文件

批量网文件夹中写入文件,文件夹下可以是文件夹列表,批量写入java执行...查看更多
知乎模拟登陆apache.http然后jsoup爬取信息,用到的jar包都打包了,不需要再寻找

知乎模拟登陆apache.http然后jsoup爬取信息,用到的jar包都打包了,不需要再寻找...查看更多
(0) 回复