Results 1 to 6 of 6
  1. #1
    Jovan is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default Save File Function

    Hello to everyone... i'm beginner in Java programing and i got a question for you... so i want to make some app. like notepad. i have written my codes for the gui, but i need the code to save the file... please help !!!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    You could use Search or Google to find some java code samples that use the println() method.

  3. #3
    Jovan is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default

    i have tried with that, but i can't find... if you have found a link, please send me

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    Perhaps println() finds too many. It's often used with the System.out object.

    Try PrintWriter instead. I just did a Search here and got over 400 hits. Some will have sample code.

  5. #5
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default JFileChooser plus a File Reader/Writer

    I found this extremely useful code on the internet.
    It helped me understand the JFileChooser far better
    than the java tutorials could.

    Plus, it has functional File Read and Write.
    Plus, it's so easy to analyze.

    I wish I could remember who to give credit to
    for its creation.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    
    public class FileDialogDemo extends JFrame implements ActionListener{
      private JMenuItem jmiOpen;
      private JMenuItem jmiSave;
      private JMenuItem jmiExit;
      private JMenuItem jmiAbout;
      private JTextArea jta = new JTextArea();
      private JLabel jlblStatus = new JLabel();
      private JFileChooser jFileChooser = new JFileChooser();
      
      public static void main(String [] args){
        FileDialogDemo frame = new FileDialogDemo();
        frame.setLocation(100, 200);
        frame.setTitle("Test JFileChooser");
        frame.setSize(300,150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    
      public FileDialogDemo(){
        JMenuBar mb = new JMenuBar();
        setJMenuBar(mb);
    
        JMenu fileMenu = new JMenu("File");
        mb.add(fileMenu);
    
        JMenu helpMenu = new JMenu("Help");
        mb.add(helpMenu);
    
        fileMenu.add(jmiOpen = new JMenuItem("Open"));
        fileMenu.add(jmiSave = new JMenuItem("Save"));
        fileMenu.addSeparator();
        fileMenu.add(jmiExit = new JMenuItem("Exit"));
        helpMenu.add(jmiAbout = new JMenuItem("About"));
    
        jFileChooser.setCurrentDirectory(new File("."));
    
        getContentPane().add(new JScrollPane(jta), BorderLayout.CENTER);
        getContentPane().add(jlblStatus, BorderLayout.SOUTH);
    
        jmiOpen.addActionListener(this);
        jmiSave.addActionListener(this);
        jmiExit.addActionListener(this);
        jmiAbout.addActionListener(this);
      }
      
    
      public void actionPerformed(ActionEvent e){
        String actionCommand = e.getActionCommand();
    
        if (e.getSource() instanceof JMenuItem){
          if ("Open" .equals(actionCommand)){
            open();
          }
          else if ("Save" .equals(actionCommand)){
            save();
          }
          else if ("About" .equals(actionCommand)){
            JOptionPane.showMessageDialog(this, "Demonstrate Using File Dialogs",
              "About this Demo", JOptionPane.INFORMATION_MESSAGE);
          }
          else if ("Exit" .equals(actionCommand)){
            System.exit(0);
          }
        }
      }
    
      private void open(){
        if (jFileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
          open(jFileChooser.getSelectedFile());
        }
      }
    
      private void open(File file){
        try{
          BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
          byte [] b = new byte[in.available()];
          in.read(b, 0, b.length);
          jta.append(new String(b, 0, b.length));
          in.close();
    
          jlblStatus.setText(file.getName() + " opened");
        }
        
        catch(IOException ex){
          jlblStatus.setText("Error opening file " + file.getName());
        }
      }
    
      private void save(){
        if (jFileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
          save(jFileChooser.getSelectedFile());
        }
      }
    
      private void save(File file){
        try{
          BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
          byte []  b = (jta.getText()).getBytes();
          out.write(b, 0, b.length);
          out.close();
    
          jlblStatus.setText(file.getName() + " saved ");
        }
    
        catch (IOException ex){
          jlblStatus.setText("Error saving " + file.getName());
        }
      }
    }

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default

    I find a PrintWriter easier to use than an OutputStream. The PrintWriter class has a println() method that will write Strings and end them with a lineend.
    The save() method has to convert the String to bytes and then write then vs just using println(String) to write the string.

    The java file I/O classes are confusing at first because they can do it so many different ways.

Similar Threads

  1. save file based on file extension
    By masa in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2010, 11:17 AM
  2. want to save JTable in a file..
    By doha786 in forum New To Java
    Replies: 4
    Last Post: 04-10-2010, 08:37 AM
  3. Replies: 0
    Last Post: 03-05-2010, 04:21 AM
  4. Save file
    By dejos456 in forum New To Java
    Replies: 4
    Last Post: 11-28-2009, 03:13 PM
  5. how to save file..
    By jont717 in forum New To Java
    Replies: 2
    Last Post: 02-12-2009, 11:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •