文件操作类


//获取系统当前时间
public static String currentlyTime() {
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
return dateFormat.format(date);
}


// 创建文件夹
public File createFile(String path) {
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
return file;
}


// 复制文件夹
public boolean copyFile(String source_path, String dest_path) {
boolean flag = false;
createFile(dest_path);
File[] file_source = new File(source_path).listFiles();
for (int i = 0; i < file_source.length; i++) {
if (file_source[i].isFile()) {
try {
FileInputStream input = new FileInputStream(file_source[i]);
FileOutputStream output = new FileOutputStream(dest_path
+ "/" + file_source[i].getName());
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) > 0) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
}
// 如果发现源目录下包含子目录的话,则递归调用copyFile()方法;
if (file_source[i].isDirectory()) {
copyFile(source_path + "/" + file_source[i].getName(),
dest_path + "/" + file_source[i].getName());
}
}
flag = true;
}
return flag;
}


// 读流
public byte[] readStream(String path) {
FileInputStream fileinputstream = null;
byte[] bytes = null;
try {
fileinputstream = new FileInputStream(path);
bytes = new byte[1024 * 5];
fileinputstream.read(bytes);
fileinputstream.close();
} catch (Exception e) {
System.out.println(e);
}
return bytes;
}


// 写流
public void writeStream(String template, String path, Integer id) {
File file = new File(path);
if (!file.exists()) {
file.mkdir();
}
path = path + "/content" + id + ".html";
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(path);
byte tag_bytes[] = template.getBytes();
fileOutputStream.write(tag_bytes);
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
System.out.println(e);
}
}


//java重构 writeStream
public void writeStream(String template, String path, String net_page) {
File file = new File(path);
if (!file.exists()) {
file.mkdir();
}
if (net_page.split("/").length > 0) {
net_page = net_page.substring(net_page.lastIndexOf("/") + 1);
}
path = path + "/" + net_page;
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(path);
byte tag_bytes[] = template.getBytes();
fileOutputStream.write(tag_bytes);
fileOutputStream.close();
fileOutputStream.flush();
} catch (Exception e) {
System.out.println(e);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值