含读写及替换字符的复制文件操作
2007-05-30 12:10
501 查看
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class ChangeFile {
/**
* 复制整个文件夹内容,并替换所有文件中的";"
*
* @param oldPath
* String 原文件路径 如:e:/b/
* @param newPath
* String 复制后路径 如:e:/a/
*/
public 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()) {
this.writeFile(this.readerFile(temp.toString()), newPath
+ "/" + (temp.getName()).toString());
}
if (temp.isDirectory()) {// 如果是子文件夹
copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
}
}
System.out.println("复制文件夹操作 成功执行");
} catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
}
public String readerFile(String filePath) {
String a = "";
File f = new File(filePath);
if (f.exists()) {
try {
// 下为读取文件
BufferedReader raf = new BufferedReader(
new FileReader(filePath));
char[] buf = new char[1024 * 5];
int len = raf.read(buf);
a = new String(buf, 0, len);
} catch (Exception e) {
e.printStackTrace();
}
}
return a.replaceAll(";", " ");
}
public String readerFile(String filePath) {
StringBuffer sb = new StringBuffer();
File f = new File(filePath);
if (f.exists()) {
try {
// 下为读取文件
BufferedReader raf = new BufferedReader(
new FileReader(filePath));
String line = "";
while ((line = raf.readLine()) != null) {
if (line.trim().length() > 0) {
sb.append(line + "/r/n");
}
}
raf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
String b = sb.toString();
b = b.substring(0, b.length() - 2);
return b.replaceAll(";", " ");
}
public static void writeFile(String content, String filePath) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(filePath));
bw.write(content);
bw.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (Exception ex) {
}
bw = null;
}
}
}
public static void main(String[] args) {
ChangeFile a = new ChangeFile();
a.copyFolder("e:/b/", "e:/a/");
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class ChangeFile {
/**
* 复制整个文件夹内容,并替换所有文件中的";"
*
* @param oldPath
* String 原文件路径 如:e:/b/
* @param newPath
* String 复制后路径 如:e:/a/
*/
public 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()) {
this.writeFile(this.readerFile(temp.toString()), newPath
+ "/" + (temp.getName()).toString());
}
if (temp.isDirectory()) {// 如果是子文件夹
copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
}
}
System.out.println("复制文件夹操作 成功执行");
} catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
}
public String readerFile(String filePath) {
String a = "";
File f = new File(filePath);
if (f.exists()) {
try {
// 下为读取文件
BufferedReader raf = new BufferedReader(
new FileReader(filePath));
char[] buf = new char[1024 * 5];
int len = raf.read(buf);
a = new String(buf, 0, len);
} catch (Exception e) {
e.printStackTrace();
}
}
return a.replaceAll(";", " ");
}
public String readerFile(String filePath) {
StringBuffer sb = new StringBuffer();
File f = new File(filePath);
if (f.exists()) {
try {
// 下为读取文件
BufferedReader raf = new BufferedReader(
new FileReader(filePath));
String line = "";
while ((line = raf.readLine()) != null) {
if (line.trim().length() > 0) {
sb.append(line + "/r/n");
}
}
raf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
String b = sb.toString();
b = b.substring(0, b.length() - 2);
return b.replaceAll(";", " ");
}
public static void writeFile(String content, String filePath) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(filePath));
bw.write(content);
bw.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (Exception ex) {
}
bw = null;
}
}
}
public static void main(String[] args) {
ChangeFile a = new ChangeFile();
a.copyFolder("e:/b/", "e:/a/");
}
}
相关文章推荐
- ASP FSO操作文件(复制文件、重命名文件、删除文件、替换字符
- ASP FSO操作文件(复制文件、重命名文件、删除文件、替换字符
- 【Java】File类文件管理及IO读写、复制操作
- 【IPHONE开发-OBJECTC入门学习】文件的操作,读写复制文件
- 批处理文件操作:替换文本文件某系字符写入新文件、遍历目录并进行字符串替换、拷贝文件到新目录、去掉文件扩展名
- 【IPHONE开发-OBJECTC入门学习】文件的操作,读写复制文件
- 【java】:对属性文件的读写操作,中文字符解决方案
- 【C/C++学院】0826-文件重定向/键盘输入流/屏幕输出流/字符串输入输出/文件读写简单操作/字符文件读写二进制与文本差别/get与getline挖掘数据/二进制与文本差别/随机位置/多线程初级
- Win8 Metro中文件读写删除与复制操作
- 文件操作《2》字符读写
- C:文件操作-读写字符与读写行(!!!好)
- (字节流、字符流)对文件进行读写操作
- java 字符流实现文件读写操作(FileReader-FileWriter)
- Java File类文件管理及IO读写、复制操作
- 【IPHONE开发-OBJECTC入门学习】文件的操作,读写复制文件
- 示例1.7 对字符文件的读写操作
- 【Java基础知识】IO类--字符流读写数据以及复制文件的几种方式
- 流的操作(复制文件) 读写文件
- 黑马程序员—static和extern及文件操作和字符读写
- 129.(字节流、字符流)对文件进行读写操作