java读取文件夹下的所有txt文件_求代码!! java如何读取文件夹中所有txt文档(包含子文件夹中的txt文档)...

展开全部

/**

* 复制整个目录的内容

*

* @param srcDirName

*            待复制目录的目录名e5a48de588b662616964757a686964616f31333337396330

* @param destDirName

*            目标目录名

* @param overlay

*            如果目标目录存在,是否覆盖

* @return 如果复制成功返回true,否则返回false

*/

public static boolean copyDirectory(String srcDirName, String destDirName,

boolean overlay) {

// 判断源目录是否存在

File srcDir = new File(srcDirName);

if (!srcDir.exists()) {

MESSAGE = "复制目录失败:源目录" + srcDirName + "不存在!";

JOptionPane.showMessageDialog(null, MESSAGE);

return false;

} else if (!srcDir.isDirectory()) {

MESSAGE = "复制目录失败:" + srcDirName + "不是目录!";

JOptionPane.showMessageDialog(null, MESSAGE);

return false;

}

// 如果目标目录名不是以文件分隔符结尾,则加上文件分隔符

if (!destDirName.endsWith(File.separator)) {

destDirName = destDirName + File.separator;

}

File destDir = new File(destDirName);

// 如果目标文件夹存在

if (destDir.exists()) {

// 如果允许覆盖则删除已存在的目标目录

if (overlay) {

new File(destDirName).delete();

} else {

MESSAGE = "复制目录失败:目的目录" + destDirName + "已存在!";

JOptionPane.showMessageDialog(null, MESSAGE);

return false;

}

} else {

// 创建目的目录

System.out.println("目的目录不存在,准备创建。。。");

if (!destDir.mkdirs()) {

System.out.println("复制目录失败:创建目的目录失败!");

return false;

}

}

boolean flag = true;

File[] files = srcDir.listFiles();

for (int i = 0; i 

// 复制文件

if (files[i].isFile()) {

flag = CopyFileUtil.copyFile(files[i].getAbsolutePath(),

destDirName + files[i].getName(), overlay);

if (!flag)

break;

} else if (files[i].isDirectory()) {

flag = CopyFileUtil.copyDirectory(files[i].getAbsolutePath(),

destDirName + files[i].getName(), overlay);

if (!flag)

break;

}

}

if (!flag) {

MESSAGE = "复制目录" + srcDirName + "至" + destDirName + "失败!";

JOptionPane.showMessageDialog(null, MESSAGE);

return false;

} else {

return true;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题需要更具体的信息,因为读取文件夹中某个文档的数据取决于你使用的编程语言和操作系统。以下是一些可能有用的信息: - Python:你可以使用 `os` 模块的 `listdir` 函数获取文件夹的所有文件列表,然后使用 `open` 函数打开指定的文档并读取其的数据。例如: ``` import os dir_path = '/path/to/folder' file_name = 'example.txt' files = os.listdir(dir_path) if file_name in files: file_path = os.path.join(dir_path, file_name) with open(file_path, 'r') as f: data = f.read() print(data) else: print('File not found') ``` - Java:你可以使用 `File` 类和 `FileReader` 类来读取文件夹中文档数据。例如: ``` import java.io.File; import java.io.FileReader; import java.io.IOException; public class ReadFile { public static void main(String[] args) throws IOException { String dirPath = "/path/to/folder"; String fileName = "example.txt"; File dir = new File(dirPath); if (dir.isDirectory()) { File file = new File(dir, fileName); if (file.isFile()) { FileReader reader = new FileReader(file); char[] buffer = new char[1024]; int read = reader.read(buffer); String data = new String(buffer, 0, read); System.out.println(data); reader.close(); } else { System.out.println("File not found"); } } else { System.out.println("Folder not found"); } } } ``` - C++:你可以使用 `dirent.h` 库来读取文件夹中文档数据。例如: ``` #include <iostream> #include <dirent.h> #include <fstream> #include <string> using namespace std; int main() { string folder_path = "/path/to/folder"; string file_name = "example.txt"; DIR* dir = opendir(folder_path.c_str()); if (dir) { dirent* entry; while ((entry = readdir(dir)) != NULL) { if (entry->d_name == file_name) { string file_path = folder_path + "/" + file_name; ifstream file(file_path); if (file.is_open()) { string data((istreambuf_iterator<char>(file)), istreambuf_iterator<char>()); cout << data << endl; file.close(); } else { cout << "File not found" << endl; } break; } } closedir(dir); } else { cout << "Folder not found" << endl; } return 0; } ``` 请注意,这些示例代码仅供参考,你需要根据自己的具体情况进行修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值