黑马程序员笔记:编写程序实现指定文件的复制粘贴
来源:互联网 发布:独立域名什么意思 编辑:程序博客网 时间:2024/05/16 19:46
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Test9 {
/**
*
* 编写程序, 将指定目录下所有.java文件拷贝到另一个目的中,并将扩展名改为.txt
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// 创建Scanner对象接收键盘输入源路径和目标路径
Scanner scanner = new Scanner(System.in);
// 提示输入源路径
System.out.println("请输入源路径:");
String sourcePath = scanner.nextLine();
// 提示输入目标路径
System.out.println("请输入目标路径:");
String targetPath = scanner.nextLine();
// 获取源路径
File source = new File(sourcePath);
// 判断源路径是否存在,如果不存在,报错并返回
if (!source.exists()) {
System.out.println("该源路径不存在");
return;
}
// 获取目标路径
File target = new File(targetPath);
// 判断目标路径是否存在,如果不存在则创建该路径
if (!target.exists()) {
target.mkdirs();
}
// 调用拷贝方法
copyAllJava(source, target);
System.out.println("拷贝成功");
}
private static void copyAllJava(File source, File target)
throws IOException {
// TODO Auto-generated method stub
// 获取源路径中的所有文件
File[] sourceFileList = source.listFiles();
// 遍历所有文件
for (int i = 0; i < sourceFileList.length; i++) {
// 如果文件是*.java文件,则复制到目标路径,并该为*.txt文件
if (sourceFileList[i].getName().endsWith(".java")) {
File targetFile = new File(target, sourceFileList[i].getName().replace(
".java", ".txt"));
BufferedReader bufReader = null;
BufferedWriter bufWriter = null;
try {
bufReader = new BufferedReader(new FileReader(sourceFileList[i]));
bufWriter = new BufferedWriter(new FileWriter(targetFile));
String str = "";
while ((str = bufReader.readLine()) != null) {// 运用字符缓冲流,复制java文件进入目标路径下同名txt文件.
bufWriter.write(str);
bufWriter.newLine();
}
} catch (IOException e) {
throw e;
} finally {// 关闭资源.
if (bufWriter != null)
try {
bufWriter.close();
} catch (IOException e) {
throw e;
} finally {
if (bufReader != null)
try {
bufReader.close();
} catch (IOException e) {
throw e;
}
}
}
}
// 如果文件是文件夹,则采用递归方法遍历该文件夹
if (sourceFileList[i].isDirectory()) {
File targetFiles = new File(target, sourceFileList[i].getName());
targetFiles.mkdirs();
copyAllJava(sourceFileList[i], targetFiles);
}
}
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Test9 {
/**
*
* 编写程序, 将指定目录下所有.java文件拷贝到另一个目的中,并将扩展名改为.txt
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// 创建Scanner对象接收键盘输入源路径和目标路径
Scanner scanner = new Scanner(System.in);
// 提示输入源路径
System.out.println("请输入源路径:");
String sourcePath = scanner.nextLine();
// 提示输入目标路径
System.out.println("请输入目标路径:");
String targetPath = scanner.nextLine();
// 获取源路径
File source = new File(sourcePath);
// 判断源路径是否存在,如果不存在,报错并返回
if (!source.exists()) {
System.out.println("该源路径不存在");
return;
}
// 获取目标路径
File target = new File(targetPath);
// 判断目标路径是否存在,如果不存在则创建该路径
if (!target.exists()) {
target.mkdirs();
}
// 调用拷贝方法
copyAllJava(source, target);
System.out.println("拷贝成功");
}
private static void copyAllJava(File source, File target)
throws IOException {
// TODO Auto-generated method stub
// 获取源路径中的所有文件
File[] sourceFileList = source.listFiles();
// 遍历所有文件
for (int i = 0; i < sourceFileList.length; i++) {
// 如果文件是*.java文件,则复制到目标路径,并该为*.txt文件
if (sourceFileList[i].getName().endsWith(".java")) {
File targetFile = new File(target, sourceFileList[i].getName().replace(
".java", ".txt"));
BufferedReader bufReader = null;
BufferedWriter bufWriter = null;
try {
bufReader = new BufferedReader(new FileReader(sourceFileList[i]));
bufWriter = new BufferedWriter(new FileWriter(targetFile));
String str = "";
while ((str = bufReader.readLine()) != null) {// 运用字符缓冲流,复制java文件进入目标路径下同名txt文件.
bufWriter.write(str);
bufWriter.newLine();
}
} catch (IOException e) {
throw e;
} finally {// 关闭资源.
if (bufWriter != null)
try {
bufWriter.close();
} catch (IOException e) {
throw e;
} finally {
if (bufReader != null)
try {
bufReader.close();
} catch (IOException e) {
throw e;
}
}
}
}
// 如果文件是文件夹,则采用递归方法遍历该文件夹
if (sourceFileList[i].isDirectory()) {
File targetFiles = new File(target, sourceFileList[i].getName());
targetFiles.mkdirs();
copyAllJava(sourceFileList[i], targetFiles);
}
}
}
}
0 0
- 黑马程序员笔记:编写程序实现指定文件的复制粘贴
- 实现文件的复制粘贴
- 打开指定的应用程序+实现复制粘贴
- 黑马程序员-----JAVA查找文件,复制到指定的文件夹
- Clipboard实现文件,文本的"复制","粘贴"
- 黑马程序员_某目录下的指定文件复制到指定文件夹内的操作
- 复制粘贴的实现
- 习题12:编写程序,将文件复制到指定路径,允许改写同名的目标文件。
- 程序中的剪切、复制和粘贴的实现问题解说
- 如何实现vim和其他程序的复制粘贴
- 黑马程序题--文件的复制
- 黑马程序员-学习笔记2-IO流之文件复制
- 黑马程序员----FileWriter与FileReader实现文件复制
- 黑马程序员------IO流文件复制4种代码实现
- 黑马程序员 编写一个程序,将d:\java目录下的所有.java文件复制到d:\jad目录下,并将原来文件的扩展名从.java改为.jad。
- 黑马程序员_文件复制
- 使用剪贴板的复制/粘贴程序
- 黑马程序员—编写程序,将指定目录下所有.java文件拷贝到另一个目的中,并将扩展名改为.txt
- loadrunner is so wonderful
- HTML5 Canvas 获得鼠标的移动的坐标
- loadrunner is so wonderful
- Android之Handler用法总结
- 伤不起的全栈程序员-转给不知道何去何从的菜鸟程序员们
- 黑马程序员笔记:编写程序实现指定文件的复制粘贴
- NSDate 时间比较
- HTML5 地理定位
- loadrunner is so wonderful
- Hibernate 之 增删改查
- 单向链表高效的实现栈类,不含链表头和尾节点~使用单项链表实现高效的队类,不含链表头和尾节点
- NSDate
- EditText 不可编辑状态的实现
- XGantt全局视图与详细视图模式的应用