Cómo abrir archivos en java?
Publicado por Martha (7 intervenciones) el 17/09/2006 07:11:06
Hola a todos de nuevo, quisiera saber cómo abrir archivos en java. Ya tengo el código para guardarlos pero no se como abrirlos...
public class CenterPanel extends JPanel implements ActionListener
{ // Opens class
private JTextArea comments;
private JScrollPane scrollpane;
private JButton saveAs;
private JLabel whiteshark;
private Box box;
public CenterPanel()
{ // open constructor
setBackground(Color.white);
comments = new JTextArea();
comments.setLineWrap(true);
comments.setWrapStyleWord(true);
comments.setEditable(true);
comments.setFont(new Font("Times-Roman", Font.PLAIN, 14));
scrollpane = new JScrollPane(comments);
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//saveAs = new JButton("Save Comments");
//saveAs.addActionListener( this );
whiteshark = new JLabel("",
new ImageIcon("images/gwhite.gif"),
JLabel.CENTER);
box = Box.createVerticalBox();
box.add(scrollpane);
box.add(Box.createVerticalStrut(10));
//box.add(saveAs);
box.add(Box.createVerticalStrut(15));
box.add(whiteshark);
add(box);
} // closes constructor
public void actionPerformed( ActionEvent evt )
{ // open method
JFileChooser jfc = new JFileChooser();
jfc.setSize(400, 300);
Container parent = saveAs.getParent();
int choice = jfc.showSaveDialog(parent); //AQUÍ SE CAMBIA POR SHOWOPENDIALOG
if (choice == JFileChooser.APPROVE_OPTION) //LO QUE NO SE ES LO QUE VIENE AQUI
{
String filename = jfc.getSelectedFile().getAbsolutePath();
try
{ // opens try
BufferedWriter bw = new BufferedWriter(new FileWriter(filename));
bw.write(comments.getText());
bw.flush();
bw.close();
} // closes try
catch (IOException ioe)
{ // open catch
}// close catch
}//close if statement
} //close method*/
} // Closes class
Si alguien me puede ayudar, se lo agradecería mucho.
public class CenterPanel extends JPanel implements ActionListener
{ // Opens class
private JTextArea comments;
private JScrollPane scrollpane;
private JButton saveAs;
private JLabel whiteshark;
private Box box;
public CenterPanel()
{ // open constructor
setBackground(Color.white);
comments = new JTextArea();
comments.setLineWrap(true);
comments.setWrapStyleWord(true);
comments.setEditable(true);
comments.setFont(new Font("Times-Roman", Font.PLAIN, 14));
scrollpane = new JScrollPane(comments);
scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//saveAs = new JButton("Save Comments");
//saveAs.addActionListener( this );
whiteshark = new JLabel("",
new ImageIcon("images/gwhite.gif"),
JLabel.CENTER);
box = Box.createVerticalBox();
box.add(scrollpane);
box.add(Box.createVerticalStrut(10));
//box.add(saveAs);
box.add(Box.createVerticalStrut(15));
box.add(whiteshark);
add(box);
} // closes constructor
public void actionPerformed( ActionEvent evt )
{ // open method
JFileChooser jfc = new JFileChooser();
jfc.setSize(400, 300);
Container parent = saveAs.getParent();
int choice = jfc.showSaveDialog(parent); //AQUÍ SE CAMBIA POR SHOWOPENDIALOG
if (choice == JFileChooser.APPROVE_OPTION) //LO QUE NO SE ES LO QUE VIENE AQUI
{
String filename = jfc.getSelectedFile().getAbsolutePath();
try
{ // opens try
BufferedWriter bw = new BufferedWriter(new FileWriter(filename));
bw.write(comments.getText());
bw.flush();
bw.close();
} // closes try
catch (IOException ioe)
{ // open catch
}// close catch
}//close if statement
} //close method*/
} // Closes class
Si alguien me puede ayudar, se lo agradecería mucho.
Valora esta pregunta


0