Java 如何使用JFileChooser保存txt文件?
鉴于此方法:Java 如何使用JFileChooser保存txt文件?,java,swing,file,jfilechooser,Java,Swing,File,Jfilechooser,鉴于此方法: public void OutputWrite (BigInteger[] EncryptCodes) throws FileNotFoundException{ JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.showSaveDialog(null); String
public void OutputWrite (BigInteger[] EncryptCodes) throws FileNotFoundException{
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.showSaveDialog(null);
String path = chooser.getSelectedFile().getAbsolutePath();
PrintWriter file = new PrintWriter(new File(path+"EncryptedMessage.txt"));
for (int i = 0; i <EncryptCodes.length; i++) {
file.write(EncryptCodes[i]+ " \r\n");
}
file.close();
}
public void OutputWrite(BigInteger[]EncryptCodes)引发FileNotFoundException{
JFileChooser chooser=新的JFileChooser();
setFileSelectionMode(仅限于JFileChooser.DIRECTORIES_);
chooser.showSaveDialog(空);
字符串路径=chooser.getSelectedFile().getAbsolutePath();
PrintWriter文件=新的PrintWriter(新文件(路径+“EncryptedMessage.txt”);
对于(inti=0;i像这样
PrintWriter file = new PrintWriter(new File(filePathChosenByUser + "EncryptedMessage.txt"));
像这样
PrintWriter file = new PrintWriter(new File(filePathChosenByUser + "EncryptedMessage.txt"));
您可以添加一个单独的方法来获取保存位置,如下所示:
private File getSaveLocation() {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = chooser.showSaveDialog(this);
if (result == chooser.APPROVE_OPTION) {
return chooser.getSelectedFile();
} else {
return null;
}
}
然后将结果用作重载的文件
构造函数的参数,该构造函数接受父/目录参数:
public void writeOutput(File saveLocation, BigInteger[] EncryptCodes)
throws FileNotFoundException {
PrintWriter file =
new PrintWriter(new File(saveLocation, "EncryptedMessage.txt"));
...
}
您可以添加一个单独的方法来获取保存位置,如下所示:
private File getSaveLocation() {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = chooser.showSaveDialog(this);
if (result == chooser.APPROVE_OPTION) {
return chooser.getSelectedFile();
} else {
return null;
}
}
然后将结果用作重载的文件
构造函数的参数,该构造函数接受父/目录参数:
public void writeOutput(File saveLocation, BigInteger[] EncryptCodes)
throws FileNotFoundException {
PrintWriter file =
new PrintWriter(new File(saveLocation, "EncryptedMessage.txt"));
...
}
文件路径应该在运行时由用户选择好,然后您必须在运行时填写filePathChosenByUser
?如果我理解正确,您需要用户在请求时实际编写路径,我需要的是在运行时打开一个对话框,让用户选择目录我很确定您是否实现了一个浏览器,它将能够以字符串的形式返回所选的路径。那么我们回到问题上来了。我该怎么做?文件路径应该在运行时由用户选择好,那么您必须在运行时填写filepath chosenbyuser
?如果我理解正确,您需要用户在请求时实际编写路径,我需要什么d是一个在运行时打开的对话框,用户可以选择目录。我很确定,如果您实现了资源管理器,它将能够以字符串形式返回所选路径。我们回到问题上来。我该如何做?是的。很好,但是短语“this”出现错误。类JFileChooser中的方法showSaveDialog无法应用于给定类型;必需:找到组件:RSA加密原因:实际参数RSA加密无法通过方法调用转换转换为组件——好的,您需要一个JFrame
参数。如果您仅使用JFileChooser
在控制台应用程序中,您只需在此处使用null
。是的..完美,但短语“this”出现错误。类JFileChooser中的方法showSaveDialog无法应用于给定类型;必需:找到组件:RSA加密原因:实际参数RSA加密无法通过方法调用转换转换为组件——好的,您需要一个JFrame
参数。如果您仅使用JFileChooser
在控制台应用程序中,您只需在此处使用null
。