我正在尝试使用以下代码从Apache Commons io(2.4)使用FileUtils方法“ static void copyDirectory(File srcDir,File destDir)”将文件从一个文件夹复制到另一个文件夹:
String srcDir = "/sourceDirectory/examples/";
String destDir = "/tmp/examples/";
try{
FileUtils.copyDirectory(new File(srcDir), new File(destDir));
} catch (IOException e){
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
文件结构如下所示:
examples/ (read access)
examples/.subdirectory (NO access)
examples/file1.txt (read access)
examples/file2.txt (read access)
examples/subdirectory2/file1.txt (read access)
examples/subdirectory2/file2.txt (NO access)
Run Code Online (Sandbox Code Playgroud)
问题是,在srcDir中有一些文件和一个我无权访问的子目录。FileUtils.copyDirectory抛出“ java.io.FileNotFoundException:.subdirectory(权限被拒绝)”并中止。
是否可以忽略我无权访问的那些文件,而仅复制我可以读取的所有其他文件?
我正在尝试使用Apache Commons IO的FileUtils.writeStringToFile()方法.每一份文档都说我可以这样做:
FileUtils.writeStringToFile(File, String with data, boolean append);
Run Code Online (Sandbox Code Playgroud)
我想要这种方法,因为我希望每次都将数据写入文件的末尾.
但是,在Eclipse中,它一直告诉我这个方法不存在.我唯一的两个是:
FileUtils.writeStringToFile(File, String with data);
FileUtils.writeStringToFile(File, String with data, String encoding);
Run Code Online (Sandbox Code Playgroud)
我纠正了我的POM文件,现在有这种依赖:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我我做错了什么吗?
我在Rails中有一个控制器,其动作用于创建一个新目录.
此操作应创建目录"/ public/graph_templates/aaa/test".但是它会从最终目录"test"中删除.为什么这只创建父目录?
def create_temporary_template
dir = File.dirname("#{Rails.root}/public/graph_templates/aaa/test")
FileUtils.mkdir_p dir
end
Run Code Online (Sandbox Code Playgroud)
文档:http://ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html#method-c-mkdir_p
我有一个包含文本(乌克兰符号)的文件。例子:${name} - \xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd1\x96\xd1\x82。我需要替换从${name}到\xd0\xa1\xd0\xb0\xd1\x88\xd0\xb0。但在输出文件中,我收到类似这样的内容:\xc3\x90\xc2\xbf\xc3\x91??\xc3\x90\xc2\xb8\xc3\x90\xc2\xb2\xc3\x90\xc2\xb5\xc3\x91 - \xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd1\x96\xd1\x82而不是\xd0\xa1\xd0\xb0\xd1\x88\xd0\xb0- \xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd1\x96\xd1\x82. 但如果我使用 .txt 而不是 .rtf - 一切都很好。我理解编码中的问题,但无法解决它。
代码示例:
\n\nFile file = new File("original.rtf");\nString content = FileUtils.readFileToString(file);\n\ncontent = content.replace("${name}", "\xd0\xa1\xd0\xb0\xd1\x88\xd0\xb0");\n\nFile fileNew = new File("changed.rtf");\nFileOutputStream fop = new FileOutputStream(fileNew);\n\nbyte[] contentInBytes = content.getBytes("UTF-8");\nfop.write(contentInBytes);\nfop.flush();\nfop.close();\nRun Code Online (Sandbox Code Playgroud)\n 我确信这已经得到了解答,但十种不同的策略在这个问题上没有奏效.
如果我使用C:\ Users\Anny\Dropbox\SocialMediaOcto\instructions\Trees\instructions.txt作为文件的绝对路径,则IDEA无法从此路径读取或执行.如果我采用相同的路径并将其粘贴到Windows资源管理器中,它将立即执行.我不想专注于工作目录,因为这个文件作为程序的配置文件工作,但用反斜杠替换斜杠不起作用,绝对路径仍然将我带到文件,但IDEA没有启动.
我的智慧结束了.
public static String generateFileName(String folder){
String filename = "";
List<String> hashtags = new ArrayList<>();
String instructions_file = "C:\Users\Anny\Dropbox\SocialMediaOcto\instructions\Trees\instructions.txt";
//does not return true-true, but can launch file on windows explorer..
System.out.println("FILE EXIST AND EXECUTE?" + new File(instructions_file).getAbsoluteFile().canRead() +" "+new File(instructions_file).getAbsoluteFile().canExecute());
System.out.println(new File(instructions_file).getAbsoluteFile());
//C:\Users\Anny\Dropbox\SocialMediaOcto\instructions\Trees\instructions.txt
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(new File(instructions_file).getAbsoluteFile()));
Run Code Online (Sandbox Code Playgroud)
编辑 用正斜杠替换反斜杠后,阅读器仍无法正确读取或执行该文件.
日志:字符串打印:C:/Users/Anny/Dropbox/SocialMediaOcto/instructions/Bees/instructions.txt
java.io.FileNotFoundException: C:\Users\Anny\Dropbox\SocialMediaOcto\instructions\Bees\instructions.txt (The system cannot find the file specified)
Run Code Online (Sandbox Code Playgroud) 每个人都在说使用 fileutils 将文件从 a 点移动到 b 点是多么简单,但我在移动文件时遇到了很多麻烦:(
我在 .jar 所在的目录中有一个 /temp/ 文件夹,在这个临时文件夹中我有一个 .txt 文件,我想向上移动一个目录(所以基本上在 .jar 文件旁边),但我似乎不能这样做它?
这是一些代码,但我知道它还不够接近:
public void replaceFile() {
String absolutePath = getPath();
Path from = Paths.get(absolutePath + "\\temp\\test.txt");
Path to = Paths.get(absolutePath + "\\test.txt");
try {
FileUtils.moveFile(FileUtils.getFile(from.toAbsolutePath().toString()), FileUtils.getFile(to.toAbsolutePath().toString()));
JOptionPane.showMessageDialog(null, "test");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getPath() {
File jarDir = new File(ClassLoader.getSystemClassLoader().getResource(".").getPath());
//JOptionPane.showMessageDialog(null, jarDir.getAbsolutePath());
return jarDir.getAbsolutePath();
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示感谢:\