目录
1.文本替换工具
2.完成一个文件的复制
3.完成一个文件夹的复制
1.文本替换工具
把.txt文件中的某个字符替换成另外一个字符,并放到新建的.txt文本文件中
对于a.txt,把“你” --> “我”,输出到b.txt中
import java.io.*;
import java.util.Scanner;
public class ReplaceTool {
public static void main(String[] args) throws IOException {
try (InputStream is = new FileInputStream("a.txt")){
try (Scanner scanner = new Scanner(is,"UTF-8")){
try (OutputStream os = new FileOutputStream("b.txt")){
try (Writer writer = new OutputStreamWriter(os,"UTF-8")){
try (PrintWriter printWriter = new PrintWriter(writer)){
while (scanner.hasNext()){
String line = scanner.nextLine();
String replace = line.replace("你","我");
printWriter.println(replace);
}
printWriter.flush();
}
}
}
}
}
}
}
2.完成一个文件的复制
把一个图片文件复制然后重新命名保存下来
对于1.png,复制成功后保存为2.png
import java.io.*;
public class CopyFile {
public static void main(String[] args) throws IOException {
try (InputStream is = new FileInputStream("1.png")){
try (OutputStream os = new FileOutputStream("2.png")){
byte[] arr = new byte[1024];
int n = is.read(arr);
while (true){
if (n == -1){
break;
}
os.write(arr,0,n);
}
os.flush();
}
}
}
}
3.完成一个文件夹的复制
要递归的方式对源目录进行遍历,相当于树的遍历;
对于每个节点,如果是目录,需要手动创建新的目录;如果是文件,就进行文件的复制
【D:\JAVA\java1\project\java-ee】----->【D:\JAVA\java1\project\java-ee\dest】
import java.io.*;
public class CopyDirectory {
static File srcDir = new File("D:\JAVA\java1\project\java-ee");
static File destDir = new File("D:\JAVA\java1\project\java-ee\dest");
public static void main(String[] args) throws IOException {
if (!srcDir.isDirectory()) {
System.out.println("源不是目录");
return;
}
if (destDir.exists()) {
System.out.println("目标目录已经存在");
return;
}
traversal(srcDir);
}
private static void traversal(File dir) throws IOException {
// 1. 获取目录下的所有孩子
File[] files = dir.listFiles();
if (files == null) {
return;
}
// 2. 遍历目录下的每个孩子(文件)
for (File file : files) {
File destFile = getDestFile(file);
if (file.isDirectory()) {
// 按照相同的结构,创建出目录 mkdirs()
destFile.mkdirs();
traversal(file);
} else if (file.isFile()) {
copyFile(file, destFile);
}
}
}
private static File getDestFile(File file) throws IOException {
String srcPath = srcDir.getCanonicalPath();
String filePath = file.getCanonicalPath();
String subPath = filePath.substring(srcPath.length());
String destPath = destDir.getCanonicalPath();
String destFilePath = destPath + subPath;
return new File(destFilePath);
}
private static void copyFile(File srcFile, File destFile) throws IOException {
byte[] b = new byte[1024];
try (InputStream is = new FileInputStream(srcFile)) {
try (OutputStream os = new FileOutputStream(destFile)) {
while (true) {
int n = is.read(b);
if (n == -1) {
break;
}
os.write(b, 0, n);
}
os.flush();
}
}
}
}
把.txt文件中的某个字符替换成另外一个字符,并放到新建的.txt文本文件中
对于a.txt,把“你” --> “我”,输出到b.txt中
把一个图片文件复制然后重新命名保存下来
对于1.png,复制成功后保存为2.png
import java.io.*;
public class CopyFile {
public static void main(String[] args) throws IOException {
try (InputStream is = new FileInputStream("1.png")){
try (OutputStream os = new FileOutputStream("2.png")){
byte[] arr = new byte[1024];
int n = is.read(arr);
while (true){
if (n == -1){
break;
}
os.write(arr,0,n);
}
os.flush();
}
}
}
}
3.完成一个文件夹的复制
要递归的方式对源目录进行遍历,相当于树的遍历;
对于每个节点,如果是目录,需要手动创建新的目录;如果是文件,就进行文件的复制
【D:\JAVA\java1\project\java-ee】----->【D:\JAVA\java1\project\java-ee\dest】
import java.io.*;
public class CopyDirectory {
static File srcDir = new File("D:\JAVA\java1\project\java-ee");
static File destDir = new File("D:\JAVA\java1\project\java-ee\dest");
public static void main(String[] args) throws IOException {
if (!srcDir.isDirectory()) {
System.out.println("源不是目录");
return;
}
if (destDir.exists()) {
System.out.println("目标目录已经存在");
return;
}
traversal(srcDir);
}
private static void traversal(File dir) throws IOException {
// 1. 获取目录下的所有孩子
File[] files = dir.listFiles();
if (files == null) {
return;
}
// 2. 遍历目录下的每个孩子(文件)
for (File file : files) {
File destFile = getDestFile(file);
if (file.isDirectory()) {
// 按照相同的结构,创建出目录 mkdirs()
destFile.mkdirs();
traversal(file);
} else if (file.isFile()) {
copyFile(file, destFile);
}
}
}
private static File getDestFile(File file) throws IOException {
String srcPath = srcDir.getCanonicalPath();
String filePath = file.getCanonicalPath();
String subPath = filePath.substring(srcPath.length());
String destPath = destDir.getCanonicalPath();
String destFilePath = destPath + subPath;
return new File(destFilePath);
}
private static void copyFile(File srcFile, File destFile) throws IOException {
byte[] b = new byte[1024];
try (InputStream is = new FileInputStream(srcFile)) {
try (OutputStream os = new FileOutputStream(destFile)) {
while (true) {
int n = is.read(b);
if (n == -1) {
break;
}
os.write(b, 0, n);
}
os.flush();
}
}
}
}
要递归的方式对源目录进行遍历,相当于树的遍历;
对于每个节点,如果是目录,需要手动创建新的目录;如果是文件,就进行文件的复制
【D:\JAVA\java1\project\java-ee】----->【D:\JAVA\java1\project\java-ee\dest】



