我试过找到这个问题的答案,但还没找到。我正在尝试确定如何制作临时文件的硬拷贝。我的所有搜索仅显示了如何复制标准文件。
我的程序允许用户上传文件。然后读取并分析该文件,并创建并写入新的临时文件。
接下来,我想根据用户选择将临时文件复制到实际的硬位置。
我这样做的原因是我写的实际程序曾经让用户上传文件并在开始时选择保存目的地但是我被要求让程序只询问保存目的地在最后。这就是为什么我需要一个临时文件来保存数据,直到程序结束时要求用户输入目的地。
请注意,此代码设置目标:
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);
}
}
所以我想采用File变量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");
}
}
答案 0 :(得分:1)
复制文件的推荐方法是使用Files.copy(src, dest)
。
但是我认为您希望move文件在您的情况下。
可能更好,取决于哪些资源是稀疏的,你也可以将数据保存在缓冲区中,直到你得到saveApproval
,然后才在最终目的地创建一个文件。