gpt4 book ai didi

java - 复制临时文件

转载 作者:行者123 更新时间:2023-12-01 10:35:25 24 4
gpt4 key购买 nike

我尝试过寻找这个问题的答案,但还没有找到确切的答案。我正在尝试确定如何制作临时文件的硬拷贝。我所有的搜索仅显示如何复制标准文件。

我的程序允许用户上传文件。然后读取并分析该文件,并创建并写入一个新的临时文件。

接下来我想根据用户选择将临时文件复制到实际的硬盘位置。

我这样做的原因是,我正在编写的实际程序过去常常让用户上传文件并在开始时选择保存目的地,但我被要求让程序只询问保存目的地在最后。这就是为什么我需要一个临时文件来保存数据,直到程序结束时询问用户目的地。

请注意,此代码设置目的地:

while(saveApproval==false){//While the user does not have approval of the save location..


JFileChooser chooser2 = new JFileChooser();//Creates a new JFileChooser object.

saveFile = chooser2;//Sets the save file location to chooser2.

chooser2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//User is only able to scan for
//directories.

//Completes once the user clicks okay.
int returnValue2 = chooser2.showDialog(chooser2, "Directory to save");
if(returnValue2 == JFileChooser.APPROVE_OPTION){
saveApproval=true;
}else{
System.exit(0);
}

}

所以我想获取文件变量 nachaFile 并将临时数据复制到其中。然后我想将 nachaFile 保存到用户选择的 saveFile 变量目录中。您将在下面找到我创建的完整示例代码:

package testingtemp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileWriter;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class MainClass

{

public static JFileChooser uploadFile;
public static JFileChooser saveFile;
public static boolean uploadApproval;
public static boolean saveApproval;
public static File temp;
public static File nachaFile;

public static void main(String args[]){

while(uploadApproval==false){//While upload approval has not been given..

JFileChooser chooser = new JFileChooser();//Creates a new object of the JFileChooser class.

uploadFile = chooser;//Saves the upload file variable as the chooser response.

chooser.setDialogTitle("Please choose ACH file to upload");//Sets the title bar text.

//Completes once the user clicks ok.
int returnVal = chooser.showOpenDialog(chooser);//
if(returnVal == JFileChooser.APPROVE_OPTION){
uploadApproval=true;
}else{
System.exit(0);
}

try{

File temp = File.createTempFile("NachaTemp", ".ach");

}catch(IOException e){

e.printStackTrace();

}

BufferedReader reader = null;
BufferedWriter writer = null;

try{

reader = new BufferedReader(new FileReader(uploadFile.getSelectedFile()));



writer = new BufferedWriter(new FileWriter(temp));

String line;

while((line = reader.readLine()) != null){

writer.write("1");

}

while(saveApproval==false){//While the user does not have approval of the save location..

JFileChooser chooser2 = new JFileChooser();//Creates a new JFileChooser object.

saveFile = chooser2;//Sets the save file location to chooser2.

chooser2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//User is only able to scan for
//directories.

//Completes once the user clicks okay.
int returnValue2 = chooser2.showDialog(chooser2, "Directory to save");
if(returnValue2 == JFileChooser.APPROVE_OPTION){
saveApproval=true;
}else{
System.exit(0);
}

}


}catch (IOException e){

e.printStackTrace();

}finally{

try{

reader.close();
writer.close();

}catch (IOException e){

e.printStackTrace();

}

}

}

Files.copy(temp, saveFile.getSelectedFile()+"THISISATEST.ACH");


}

}

最佳答案

复制文件的推荐方法是使用 Files.copy(src, dest) .

但是我认为你更愿意move您案例中的文件。

也许更好,具体取决于哪些资源稀缺,您还可以将数据保存在缓冲区中,直到获得 saveApproval,然后才在其最终目的地创建文件。

关于java - 复制临时文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34770779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com