Java 缓冲写入到可公开访问的位置
Java Buffered Writer to publically accessible location
我正在尝试更改我之前编写的程序。目前该程序处理一些内置逻辑的 BufferedReader 和 BufferedWriters。让我解释一下它过去是如何工作的。
Class 用于上传文件:
public void getFile(){//Used to upload the ACH file.
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.
FileNameExtensionFilter filter = new FileNameExtensionFilter("ACH Files", "ach");
//Sets the allowed file formats for upload.
chooser.setFileFilter(filter);//Activates the created file filter.
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);
}
}
}
Class用于设置目录
public void setDirectory(){//Used to set the directory.
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);
}
}
}
然后我开始实际的缓冲 reader/writer 过程,这里涉及很多逻辑:
location = "//NachaOutput"+randomNumber+".ACH";
try{
String sCurrentLine;//String representing the current line.
//Pulls the uploaded file.
br = new BufferedReader(new FileReader(NachaMain.uploadFile.getSelectedFile()));
bw = new BufferedWriter(new FileWriter(NachaMain.saveFile.getSelectedFile()+location));
现在这是我需要做的。我被要求从一开始就删除用户 selects 目录所在的屏幕。相反,用户将 select 该过程结束时的保存目录。
这意味着根本不会调用"SetDirectory"方法,因此这行代码:
bw = new BufferedWriter(new FileWriter(NachaMain.saveFile.getSelectedFile()+location));
显然行不通。我需要找到一些方法来用通用位置替换该文件编写器位置,该通用位置对于所有用户都是相同的,无论他们的设置如何。沿着文件行的东西。
我试过这样做:
bw = new BufferedWriter(new FileWriter("Libraries\Documents"+location));
但是遇到关于无效路径的异常。
所以请帮帮我,让我知道一个可以自动保存文件的好路径。保存的文件基本上是一个 "dummy" 文件。稍后在程序结束时,我会将那个虚拟文件复制到用户指定的位置,然后将其删除,所以位置真的无关紧要。
提前致谢!
您可以使用 File.createTempFile()
. It might make cleanup easier if you called deleteOnExit()
为创建的临时文件创建一个临时文件。
我正在尝试更改我之前编写的程序。目前该程序处理一些内置逻辑的 BufferedReader 和 BufferedWriters。让我解释一下它过去是如何工作的。
Class 用于上传文件:
public void getFile(){//Used to upload the ACH file.
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.
FileNameExtensionFilter filter = new FileNameExtensionFilter("ACH Files", "ach");
//Sets the allowed file formats for upload.
chooser.setFileFilter(filter);//Activates the created file filter.
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);
}
}
}
Class用于设置目录
public void setDirectory(){//Used to set the directory.
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);
}
}
}
然后我开始实际的缓冲 reader/writer 过程,这里涉及很多逻辑:
location = "//NachaOutput"+randomNumber+".ACH";
try{
String sCurrentLine;//String representing the current line.
//Pulls the uploaded file.
br = new BufferedReader(new FileReader(NachaMain.uploadFile.getSelectedFile()));
bw = new BufferedWriter(new FileWriter(NachaMain.saveFile.getSelectedFile()+location));
现在这是我需要做的。我被要求从一开始就删除用户 selects 目录所在的屏幕。相反,用户将 select 该过程结束时的保存目录。
这意味着根本不会调用"SetDirectory"方法,因此这行代码:
bw = new BufferedWriter(new FileWriter(NachaMain.saveFile.getSelectedFile()+location));
显然行不通。我需要找到一些方法来用通用位置替换该文件编写器位置,该通用位置对于所有用户都是相同的,无论他们的设置如何。沿着文件行的东西。
我试过这样做:
bw = new BufferedWriter(new FileWriter("Libraries\Documents"+location));
但是遇到关于无效路径的异常。
所以请帮帮我,让我知道一个可以自动保存文件的好路径。保存的文件基本上是一个 "dummy" 文件。稍后在程序结束时,我会将那个虚拟文件复制到用户指定的位置,然后将其删除,所以位置真的无关紧要。
提前致谢!
您可以使用 File.createTempFile()
. It might make cleanup easier if you called deleteOnExit()
为创建的临时文件创建一个临时文件。