在文本文件中写入文本并使用 saveDialog 保存
我想在文本文件中写入一个字符串,该字符串应该由保存对话框动态保存。 我已静态完成此任务,这意味着将创建具有指定文件名的文件,并且还会从 JTextArea 写入文本。我想将此文件保存在我给定的位置并使用我的给定名称。您能在这方面指导我吗?
do{
String fileData=jTextArea1.getText();
byte buf[]=fileData.getBytes();
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text/fasta files", ".txt", ".fasta");
chooser.setFileFilter(filter);
int returnVal = chooser.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("inside try after retVal");
try{
//OutputStream f2=new FileOutputStream("filename.txt");
OutputStream f2=new FileOutputStream("file.txt");
f2.write(buf);
f2.close();
} catch (IOException ex) {
Logger.getLogger(CreatingFile.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
return null;
} //else ends
// TODO add your handling code here:
}while(true);
I want to write a string in a text file which should be dynamically saved by the save dialog.
I have done this task statically, meaning the file with the specified file name is created and text is also written from JTextArea
. I want to save this file on my given location and with my given name. Can you please guide me in this regard?
do{
String fileData=jTextArea1.getText();
byte buf[]=fileData.getBytes();
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text/fasta files", ".txt", ".fasta");
chooser.setFileFilter(filter);
int returnVal = chooser.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("inside try after retVal");
try{
//OutputStream f2=new FileOutputStream("filename.txt");
OutputStream f2=new FileOutputStream("file.txt");
f2.write(buf);
f2.close();
} catch (IOException ex) {
Logger.getLogger(CreatingFile.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
return null;
} //else ends
// TODO add your handling code here:
}while(true);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Chooser.getSelectedFile()
使用包装在 BufferedWriter 中的 FileWriter,而不是 FileOutputStream。
并使用 JTextArea 的 write(...) 方法写出文本。
Use chooser.getSelectedFile()
Use a FileWriter, wrapped in a BufferedWriter, not a FileOutputStream.
And use the write(...) method of JTextArea to write out the text.
使用
而不是
Use
instead of