I am developing a java program which takes a students name and displays the name and date on a JTextPane. When the user presses exit, the program is supposed to automatically save the file in a specified directory inside a new folder created with the same name as the one the user provided for the student. Below is my code:
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();
}
}
}
}
I am running into an issue in my program where the file doesn't save to the Directory with the name provided. It consistently pops up in the console 'Directory Not Created'. Can anyone please tell me how I can fix this problem? There are no other errors in my code preventing it from running.
Thanks in advance to all who reply.
The reason you are getting this problem is because you are trying to create a directory inside ./Desktop
and you are running this from inside Documents
which doesn't have a Desktop folder. To save it to Desktop you have to use an absolute path. Absolute paths start with a /
on unix (mac and linux) and with C:
on Windows:
File f = new File("/Users/yourname/Desktop/" + name);