Results 1 to 6 of 6
Thread: Save File Function
- 06-01-2010, 10:54 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 2
- Rep Power
- 0
- 06-01-2010, 10:59 PM #2
You could use Search or Google to find some java code samples that use the println() method.
- 06-01-2010, 11:01 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 2
- Rep Power
- 0
i have tried with that, but i can't find... if you have found a link, please send me
- 06-01-2010, 11:08 PM #4
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.
- 06-02-2010, 12:27 AM #5
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
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()); } } }
- 06-02-2010, 02:27 AM #6
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
-
save file based on file extension
By masa in forum AWT / SwingReplies: 4Last Post: 05-11-2010, 11:17 AM -
want to save JTable in a file..
By doha786 in forum New To JavaReplies: 4Last Post: 04-10-2010, 08:37 AM -
How to show or open a file download or file save dialog box
By java_bond in forum New To JavaReplies: 0Last Post: 03-05-2010, 04:21 AM -
Save file
By dejos456 in forum New To JavaReplies: 4Last Post: 11-28-2009, 03:13 PM -
how to save file..
By jont717 in forum New To JavaReplies: 2Last Post: 02-12-2009, 11:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks