【问题标题】:Java File Directory IssueJava 文件目录问题
【发布时间】:2014-07-27 23:43:22
【问题描述】:

我正在开发一个 java 程序,它采用学生姓名并在 JTextPane 上显示姓名和日期。当用户按下退出时,程序应该自动将文件保存在一个新文件夹中的指定目录中,该文件夹与用户为学生提供的文件夹同名。以下是我的代码:

public class StudentRecorder extends JFrame implements ActionListener{

MyKeyListener listener;
public JTextPane page;
private JScrollPane scroll;
private JMenuBar menubar;
private AttributeSet aset;
public String name; 

private JMenu menufile;
private JMenuItem exit;

StudentRecorder(){
    super("Student Recorder");
    init();

    this.setSize(400, 400); 
    this.setLocation(400, 400);
    this.setVisible(true);
}

void init(){
    menubar = new JMenuBar();
    name = JOptionPane.showInputDialog(this, "Enter Student's Name:\n(For locations of files to be preserved, "
            + "names\nare case-sensitive.)", "Student Name", JOptionPane.QUESTION_MESSAGE);
    String timeStamp = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(Calendar.getInstance().getTime());        
    page = new JTextPane();

    if (name.equals("")){
        aset = StyleContext.getDefaultStyleContext().addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.RED);
        page.setCharacterAttributes(aset, false);
        page.setText(timeStamp + "\n" + "(Student Name Not Typed In.  You must manually save this file.  File "
                + "wont be autosaved.)" + "\n\n");
    }
    else{
        page.setText(timeStamp + "\n" + name + "\n\n");
    }

    //Declaration
    menufile = new JMenu("File");
    exit = new JMenuItem("Exit");

    //Adding to JMenuBar
    menubar.add(menufile);
    menufile.add(exit);

    //Add ActionListener
    exit.addActionListener(this);

    //Page characteristics
    aset = StyleContext.getDefaultStyleContext().addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.BLACK);
    page.setCharacterAttributes(aset, false);
    Font font = new Font("Arial", Font.PLAIN, 14);          
    page.setFont(font);
    this.setJMenuBar(menubar);
    scroll = new JScrollPane(page);
    this.add(scroll);
    scroll.createHorizontalScrollBar();     
    listener = new MyKeyListener();
    page.addKeyListener(listener);
    page.setFocusable(true);
}

@Override
public void actionPerformed(ActionEvent e) {

    if(e.getSource() == exit){
        File f = new File("./Desktop/" + name);
        try{
            if(f.mkdir()){
                System.out.println("Directory Created.");
                System.exit(0);
            }
            else{
                System.out.println("Directory Not Created.");
            }
        }catch(Exception e1){
            e1.printStackTrace();
        }

    }

}

}

我在我的程序中遇到了一个问题,即文件未使用提供的名称保存到目录中。它始终在控制台“未创建目录”中弹出。谁能告诉我如何解决这个问题?我的代码中没有其他错误阻止它运行。

提前感谢所有回复的人。

【问题讨论】:

  • 这个程序是从哪个目录运行的?
  • 它嵌套在我的文档文件夹中。我希望将我的目录保存在我的 Java 项目文件夹中,但它给了我保存“未创建目录”的声明。

标签: java file file-io directory jtextpane


【解决方案1】:

您遇到此问题的原因是因为您试图在 ./Desktop 内创建一个目录,而您是从没有 Desktop 文件夹的 Documents 内运行此目录。要将其保存到桌面,您必须使用绝对路径。绝对路径在 unix(mac 和 linux)上以 / 开头,在 Windows 上以 C: 开头:

File f = new File("/Users/yourname/Desktop/" + name);

【讨论】:

    猜你喜欢
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多