当前位置: 首页 > wzjs >正文

360建筑网站媒体发稿费用

360建筑网站,媒体发稿费用,培训医院网站建设,网站建设可用性的五个方面c和python复制java文件到指定目录 ​​1.递归遍历目录​​ 2.​扁平化复制到目标目录​​ 快速收集分散在多个子目录中的 .java 文件到一个统一目录。 备份或简单迁移Java项目文件(无需保留目录结构时)。 该代码是一个实用的Java文件收集工具&#xff0c…

c++和python复制java文件到指定目录
​​1.递归遍历目录​​
2.​扁平化复制到目标目录​​
快速收集分散在多个子目录中的 .java 文件到一个统一目录。
备份或简单迁移Java项目文件(无需保留目录结构时)。
该代码是一个实用的Java文件收集工具,适合需要快速提取所有Java文件但无需考虑目录结构的场景,使用者需注意可能存在的文件覆盖问题。

step1:

输入路径:C:\Users\wangrusheng\Downloads\Dousheng-master
输出路径:C:\JavaFilesCopyz

step2:c++
C:\Users\wangrusheng\source\repos\CMakeProject1\CMakeProject1\CMakeProject1.cpp

#include <filesystem>
#include <iostream>
#include <string>namespace fs = std::filesystem;void copy_java_files(const std::string& source_dir, const std::string& dest_dir) {try {fs::path source_path(source_dir);fs::path dest_path(dest_dir);// 创建目标根目录fs::create_directories(dest_path);// 递归遍历源目录for (const auto& entry : fs::recursive_directory_iterator(source_path)) {if (entry.is_regular_file() && entry.path().extension() == ".java") {// 直接使用文件名作为目标路径fs::path full_dest_path = dest_path / entry.path().filename();// 复制文件(覆盖已存在的)fs::copy_file(entry.path(),full_dest_path,fs::copy_options::overwrite_existing);std::cout << "已复制: " << entry.path().filename() << std::endl;}}}catch (const fs::filesystem_error& e) {std::cerr << "文件操作错误: " << e.what() << std::endl;}catch (const std::exception& e) {std::cerr << "标准异常: " << e.what() << std::endl;}catch (...) {std::cerr << "未知错误发生" << std::endl;}
}int main() {copy_java_files(R"(C:\Users\wangrusheng\Downloads\Dousheng-master)",R"(C:\JavaFilesCopys)");return 0;
}

step3:python
C:\Users\wangrusheng\PycharmProjects\FastAPIProject1\hello.py

import os
import shutil
from pathlib import Pathdef copy_java_files(source_dir: str, dest_dir: str) -> None:try:# 创建目标目录Path(dest_dir).mkdir(parents=True, exist_ok=True)# 递归遍历源目录for root, _, files in os.walk(source_dir):for file in files:if file.endswith('.java'):src_path = os.path.join(root, file)dest_path = os.path.join(dest_dir, file)# 复制文件(自动覆盖)shutil.copy2(src_path, dest_path)print(f"已复制: {file}")except PermissionError as e:print(f"权限错误: {str(e)}")except OSError as e:print(f"操作系统错误: {str(e)}")except Exception as e:print(f"未知错误: {str(e)}")if __name__ == "__main__":source = r"C:\Users\wangrusheng\Downloads\Dousheng-master"destination = r"C:\JavaFilesCopyz"copy_java_files(source, destination)

step4:复制文件内容

#include <iostream>
#include <filesystem>
#include <fstream>
#include <string>
#include <algorithm>namespace fs = std::filesystem;int main() {// 定义目标目录和输出文件路径fs::path target_dir = "C:/Users/wangrusheng/Downloads/UnoGame-main/JavaFilesCopyz";fs::path output_file = target_dir / "java_files_content.txt";// 创建并打开输出文件std::ofstream outfile(output_file);if (!outfile.is_open()) {std::cerr << "错误:无法创建输出文件!" << std::endl;return 1;}try {// 遍历目标目录for (const auto& entry : fs::directory_iterator(target_dir)) {if (entry.is_regular_file()) {// 获取文件扩展名并转换为小写std::string ext = entry.path().extension().string();std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);// 判断是否为Java文件if (ext == ".java") {std::ifstream infile(entry.path());if (infile) {// 添加文件分隔标识outfile << "=== File: " << entry.path().filename().string()<< " ===\n";// 写入文件内容outfile << infile.rdbuf() << "\n\n";infile.close();}else {std::cerr << "警告:无法读取文件 "<< entry.path() << std::endl;}}}}}catch (const fs::filesystem_error& e) {std::cerr << "文件系统错误: " << e.what() << std::endl;return 1;}std::cout << "成功合并所有Java文件内容到:" << output_file << std::endl;return 0;
}

step5:功能整合,完成版

#include <filesystem>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>namespace fs = std::filesystem;// 复制所有Java文件到目标目录
void copy_java_files(const std::string& source_dir, const std::string& dest_dir) {try {fs::path source_path(source_dir);fs::path dest_path(dest_dir);fs::create_directories(dest_path); // 确保目标目录存在for (const auto& entry : fs::recursive_directory_iterator(source_path)) {if (entry.is_regular_file() && entry.path().extension() == ".java") {fs::path full_dest_path = dest_path / entry.path().filename();fs::copy_file(entry.path(), full_dest_path, fs::copy_options::overwrite_existing);std::cout << "已复制: " << entry.path().filename() << std::endl;}}}catch (const fs::filesystem_error& e) {std::cerr << "文件操作错误: " << e.what() << std::endl;}catch (const std::exception& e) {std::cerr << "标准异常: " << e.what() << std::endl;}catch (...) {std::cerr << "未知错误发生" << std::endl;}
}// 合并Java文件内容到文本文件
void merge_java_files(const std::string& target_dir, const std::string& output_filename) {try {fs::path output_path = fs::path(target_dir) / output_filename;std::ofstream outfile(output_path);if (!outfile.is_open()) {std::cerr << "错误:无法创建输出文件!" << std::endl;return;}for (const auto& entry : fs::directory_iterator(target_dir)) {if (entry.is_regular_file()) {std::string ext = entry.path().extension().string();std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);if (ext == ".java") {std::ifstream infile(entry.path());if (infile) {outfile << "=== File: " << entry.path().filename().string() << " ===\n";outfile << infile.rdbuf() << "\n\n";infile.close();}else {std::cerr << "警告:无法读取文件 " << entry.path() << std::endl;}}}}std::cout << "成功合并所有Java文件内容到:" << output_path << std::endl;}catch (const fs::filesystem_error& e) {std::cerr << "文件系统错误: " << e.what() << std::endl;}catch (const std::exception& e) {std::cerr << "标准异常: " << e.what() << std::endl;}catch (...) {std::cerr << "未知错误发生" << std::endl;}
}int main() {const std::string source_dir = R"(C:\Users\wangrusheng\Downloads\Dousheng-master)";const std::string dest_dir = R"(C:\JavaFilesCopyx)";const std::string output_filename = "java_files_content.txt";// 第一步:复制Java文件copy_java_files(source_dir, dest_dir);// 第二步:合并文件内容merge_java_files(dest_dir, output_filename);return 0;
}

step6:python版本

import os
import shutil
from pathlib import Pathdef copy_java_files(source_dir: str, dest_dir: str) -> None:try:source_path = Path(source_dir)dest_path = Path(dest_dir)# 确保目标目录存在os.makedirs(dest_path, exist_ok=True)# 递归查找所有Java文件for java_file in source_path.rglob('*.java'):dest_file = dest_path / java_file.name# 复制并覆盖已存在的文件shutil.copy2(java_file, dest_file)print(f"已复制: {java_file.name}")except OSError as e:print(f"文件操作错误: {e}")except Exception as e:print(f"标准异常: {e}")except:print("未知错误发生")def merge_java_files(target_dir: str, output_filename: str) -> None:try:target_path = Path(target_dir)output_path = target_path / output_filenamewith open(output_path, 'w', encoding='utf-8') as outfile:# 遍历目标目录下的文件for entry in target_path.iterdir():if entry.is_file() and entry.suffix.lower() == '.java':try:with open(entry, 'r', encoding='utf-8') as infile:content = infile.read()outfile.write(f"=== File: {entry.name} ===\n")outfile.write(content + "\n\n")except OSError as e:print(f"警告:无法读取文件 {entry.name}: {e}")except Exception as e:print(f"处理文件 {entry.name} 时发生异常: {e}")print(f"成功合并所有Java文件内容到:{output_path}")except OSError as e:print(f"文件系统错误: {e}")except Exception as e:print(f"标准异常: {e}")except:print("未知错误发生")def main():source_dir = r"C:\Users\wangrusheng\Downloads\Dousheng-master"dest_dir = r"C:\JavaFilesCopyw"output_filename = "java_files_content.txt"copy_java_files(source_dir, dest_dir)merge_java_files(dest_dir, output_filename)if __name__ == "__main__":main()

end

http://www.dtcms.com/wzjs/72234.html

相关文章:

  • 商业活动的网站建设京津冀协同发展
  • 疫情北京最新消息windows优化大师有用吗
  • 平面设计图制作排名优化公司电话
  • 国外b2b免费平台武汉网络seo公司
  • 高校图书馆网站的建设方案免费seo推广计划
  • 西安的电子商城网站建设杭州百度快照优化排名推广
  • 童装网站建设文案电工培训课程
  • 常见的有利于seo的网站系统seo是什么
  • 做代购网站珠海网站建设
  • 宠物店做网站的论文网站日常维护有哪些
  • 宁波网站优化找哪家线上营销的优势
  • 石家庄网站建设seo百度电脑版入口
  • 模板建站3000是不是贵了正规的教育培训机构有哪些
  • 麻将棋牌网站开发最新国际新闻10条
  • 西宁网站建设排名阿亮seo技术
  • 正规网站建设多少费用seo怎么做优化工作
  • 网站制作策划中国经济网人事
  • 专业创业服务平台网站建设需求最新重大新闻
  • 灵宝网站建设短视频seo搜索优化
  • 个人网站如何在百度上做推广长沙seo咨询
  • 域名停靠app盘他射门下载淘宝seo优化怎么做
  • 网站关键词分割官网设计公司
  • 在线网站建设seo网络推广公司排名
  • wordpress评论数字验证码seo搜索是什么意思
  • 做进料加工在哪个网站上做搜索引擎推广渠道
  • 东莞网站建设模板设计游戏推广赚钱
  • ps做图下载网站中国国家人才培训网官网
  • 淘宝客怎么做网站管理百度网盘官网入口
  • 企业网站必须做可信网站认证湛江seo推广外包
  • 网站模板flash大数据精准营销获客