NetBeans IDE java程序未正确检索目录
我正在用Java(IDE:NetBeans 7.4、JDK 7、Swing API)编写一个程序来替代微软的蹩脚记事本 我面临的问题是,我添加了一个按钮来获取用户希望保存文件的目录。以下是按钮(名为choosedir)的代码: 上述代码在文本框“目录”中设置所选目录 现在,编写器的代码是:NetBeans IDE java程序未正确检索目录,java,swing,netbeans,java-io,jfilechooser,Java,Swing,Netbeans,Java Io,Jfilechooser,我正在用Java(IDE:NetBeans 7.4、JDK 7、Swing API)编写一个程序来替代微软的蹩脚记事本 我面临的问题是,我添加了一个按钮来获取用户希望保存文件的目录。以下是按钮(名为choosedir)的代码: 上述代码在文本框“目录”中设置所选目录 现在,编写器的代码是: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String dir=choose.getName(choose.getCurrentDirectory());
directory.setText(dir.toString());
File file=new File(dir+name+".txt");
try{
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
out.append(input.getText());
}
catch (UnsupportedEncodingException e)
{
System.out.println(e.getMessage());
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
问题是:
1.没有保存任何文件
2.无论我选择哪个目录,它都会显示Windows资源管理器中显示的目录名。比如,如果我想在C:/Windows/Temp中显示,它只显示Temp。甚至对于C:/,它也显示“本地磁盘C:”
2013年11月16日更新:尽管alex提供了帮助,该程序仍然无法运行。以下是完整的源代码:
import java.io.*;
public class QuickPad_v1 extends javax.swing.JFrame {
/**
* Creates new form QuickPad_v1
*/
public QuickPad_v1() {
initComponents();
}
/* Avoid the non-programmed buttons below! */
private void extensionActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String dir=directory.getText();
File file=new File("C://"+name+".txt");
try{
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
out.append(input.getText());
}
catch (UnsupportedEncodingException e)
{
System.out.println(e.getMessage());
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
private void choosedirActionPerformed(java.awt.event.ActionEvent evt) {
int select=choose.showOpenDialog(this);
if (select==choose.APPROVE_OPTION){
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new QuickPad_v1().setVisible(true);
}
});
}
需要帮助请!我会给你投票,保证!阅读更多关于和的使用
在您的代码中,您没有文件的完整路径,无法将其保存到所需的位置,因此它会保存…文件。getAbsoluteFile()
帮助您了解文件的位置:
String dir=choose.getName(choose.getCurrentDirectory());
directory.setText(dir.toString());
File file=new File(dir+name+".txt");
请尝试下一个代码,它可以帮助您:
String dir = choose.getCurrentDirectory().getAbsolutePath();
directory.setText(dir);
File file = new File(dir+ File.separator + name + ".txt");
了解有关使用和的更多信息
在您的代码中,您没有文件的完整路径,无法将其保存到所需的位置,因此它会保存…文件。getAbsoluteFile()
帮助您了解文件的位置:
String dir=choose.getName(choose.getCurrentDirectory());
directory.setText(dir.toString());
File file=new File(dir+name+".txt");
请尝试下一个代码,它可以帮助您:
String dir = choose.getCurrentDirectory().getAbsolutePath();
directory.setText(dir);
File file = new File(dir+ File.separator + name + ".txt");
嗨,alex,仍然无法工作。你能调试上面的源代码吗?谢谢!嗨,alex,仍然无法工作。你能调试上面的源代码吗?谢谢!