Reputation: 653
I'm writing a program in Java (IDE: NetBeans 7.4, JDK 7, Swing API) to substitute Microsoft's crappy Notepad.
The problem I'm facing is, I have added a button to fetch directory where the user wishes to save the file. Here's the code for the button (named choosedir):
private void choosedirActionPerformed(java.awt.event.ActionEvent evt) {
int select=choose.showOpenDialog(this);
if (select == choose.APPROVE_OPTION){
String dir=choose.getName(choose.getCurrentDirectory());
directory.setText(dir.toString()+"-");
}
}
The above code sets the chosen directory in the text box "directory".
Now, the code for the writer:
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());
}
}
The problem is: 1. No file is being saved 2. Whatever directory I select, it shows the directory name as seen in Windows Explorer. Like, if i want it in C:/Windows/Temp, it shows only Temp. Or even for C:/, it shows "Local Disk C:.
UPDATE 11/16/2013: Despite help from alex, the program still fails to work. Here's the entire source code:
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);
}
});
}
Help needed please! I'll give you a vote, promise!
Upvotes: 1
Views: 242
Reputation: 10994
Read more about using of JFileChooser
and its methods.
In your code you havent full path to file, for saving it where you want, because of that it saves... file.getAbsoluteFile()
helps you to know where is it:
String dir=choose.getName(choose.getCurrentDirectory());
directory.setText(dir.toString());
File file=new File(dir+name+".txt");
Try next code it helps you:
String dir = choose.getCurrentDirectory().getAbsolutePath();
directory.setText(dir);
File file = new File(dir+ File.separator + name + ".txt");
Upvotes: 1