一直困扰我的是,用Java复制文件的唯一方法是打开流,声明一个缓冲区,读取一个文件,循环遍历它,然后将其写入另一个文件.Web上充斥着类似但仍然略有不同的此类解决方案的实现.
有没有更好的方法保持在Java语言的范围内(意味着不涉及执行特定于操作系统的命令)?也许在一些可靠的开源实用程序包中,这至少会掩盖这个底层实现并提供单行解决方案?
我想将文件从一个位置复制到Java中的另一个位置.
这是我到目前为止:
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
public class TestArrayList {
public static void main(String[] args) {
File f = new File(
"D:\\CBSE_Demo\\Demo_original\\fscommand\\contentplayer\\config");
List<String>temp=new ArrayList<String>();
temp.add(0, "N33");
temp.add(1, "N1417");
temp.add(2, "N331");
File[] matchingFiles = null;
for(final String temp1: temp){
matchingFiles = f.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith(temp1);
}
});
System.out.println("size>>--"+matchingFiles.length);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这不会复制文件,执行此操作的最佳方法是什么?
我在用JDK 6.
我有2个文件夹名称Folder1和Folder2.
Folder1 有以下文件
TherMap.txt
TherMap1.txt
TherMap2.txt
Run Code Online (Sandbox Code Playgroud)
每次Folder2只有一个名称为的文件TherMap.txt.
我想要的是,
从中复制任何文件folder1并将其粘贴Folder2为TherMap.txt.如果已经TherMap.txt存在Folder2,则删除并粘贴它.
因为我写了下面的代码.但它不起作用
public void FileMoving(String sourceFilePath, String destinationPath, String fileName) throws IOException {
File destinationPathObject = new File(destinationPath);
File sourceFilePathObject = new File(sourceFilePath);
if ((destinationPathObject.isDirectory()) && (sourceFilePathObject.isFile()))
//both source and destination paths are available
{
//creating object for File class
File statusFileNameObject = new File(destinationPath + "/" + fileName);
if …Run Code Online (Sandbox Code Playgroud) 可能重复:
使用Java将文件从一个目录复制到另一个目录
如何使用java将所有文件从一个文件夹移动到其他文件夹?我正在使用此代码:
import java.io.File;
public class Vlad {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// File (or directory) to be moved
File file = new File("C:\\Users\\i074924\\Desktop\\Test\\vlad.txt");
// Destination directory
File dir = new File("C:\\Users\\i074924\\Desktop\\Test2");
// Move file to new directory
boolean success = file.renameTo(new File(dir, file.getName()));
if (!success) {
System.out.print("not good");
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它仅适用于一个特定文件.
谢谢!!!
我为管道创建了集成测试,以检查是否生成了正确的CSV文件:
class CsvBatchSinkTest {
@RegisterExtension
static SparkExtension spark = new SparkExtension();
@TempDir
static Path directory;
//this checks if the file is already available
static boolean isFileWithSuffixAvailable(File directory, String suffix) throws IOException {
return Files.walk(directory.toPath()).anyMatch(f -> f.toString().endsWith(suffix));
}
//this gets content of file
static List<String> extractFileWithSuffixContent(File file, String suffix) throws IOException {
return Files.readAllLines(
Files.walk(file.toPath())
.filter(f -> f.toString().endsWith(suffix))
.findFirst()
.orElseThrow(AssertionException::new));
}
@Test
@DisplayName("When correct dataset is sent to sink, then correct csv file should be generated.")
void testWrite() throws IOException, InterruptedException …Run Code Online (Sandbox Code Playgroud) 我正在尝试将文件从一个文件夹复制到另一个文件夹.
这是我在我的代码中得到的:
public static void copyFile(String path) throws IOException{
newPath = path;
File destination = new File ("E:/QA/chart.js");
FileUtils.copyFile(destination, new File(newPath));
}
Run Code Online (Sandbox Code Playgroud)
但它并未将所需文件复制到其位置.需要什么,从E盘复制chart.js并复制到newPath变量位置.有没有其他方法将文件从一个地方复制到另一个地方?