GoldStrike
asked on
Reading Directories and Files in Java
I have a combo box control in Java with a first element of "NEW". When i pull down the combo box menu it should display the files listed in a directory under windows. I should be able to open the file that I select and display the contents to a List Box. If I select "NEW" it should clear the contents of the List Box.
I need some code samples that I can try, which is the reason for the high points. If you've done this before or know where to get some code samples please provide information and I will award the points.
thanks
GS
I need some code samples that I can try, which is the reason for the high points. If you've done this before or know where to get some code samples please provide information and I will award the points.
thanks
GS
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
and this is ur combo listener class
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEven t ae)
{
if(ae.getSource() ==combo)
{
JComboBox cb = (JComboBox)ae.getSource();
Object newItem = cb.getSelectedItem();
try
{
FileInputStream fin=new FileInputStream(newItem.to String());
File file=new File(newItem.toString());
int length=(int)file.length();
byte bytes[]=new byte[length];
fin.read(bytes);
String string=new String(bytes);
String[] lines=string.split( "\n");
for(int i=0;i<lines.length;i++)
{
list.add(lines[i],null);
System.err.println(lines[i ]);
}
}
catch(Exception ex)
{
}
}
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEven
{
if(ae.getSource() ==combo)
{
JComboBox cb = (JComboBox)ae.getSource();
Object newItem = cb.getSelectedItem();
try
{
FileInputStream fin=new FileInputStream(newItem.to
File file=new File(newItem.toString());
int length=(int)file.length();
byte bytes[]=new byte[length];
fin.read(bytes);
String string=new String(bytes);
String[] lines=string.split( "\n");
for(int i=0;i<lines.length;i++)
{
list.add(lines[i],null);
System.err.println(lines[i
}
}
catch(Exception ex)
{
}
}
}
Why not use a JFileChooser like this? (created with netbeans)
import java.io.BufferedReader;
import javax.swing.JFileChooser;
public class FileViewer extends javax.swing.JFrame {
private javax.swing.JButton exitButton;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JButton newButton;
private javax.swing.JButton openButton;
public FileViewer() {
super("File Viewer");
initComponents();
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jPanel2 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
openButton = new javax.swing.JButton();
newButton = new javax.swing.JButton();
exitButton = new javax.swing.JButton();
addWindowListener(new java.awt.event.WindowAdapt er() {
public void windowClosing(java.awt.eve nt.WindowE vent evt) {
exitForm(evt);
}
});
jTextArea1.setPreferredSiz e(new java.awt.Dimension(300, 300));
jScrollPane1.setViewportVi ew(jTextAr ea1);
jPanel1.add(jScrollPane1);
getContentPane().add(jPane l1, java.awt.BorderLayout.CENT ER);
getContentPane().add(jPane l2, java.awt.BorderLayout.NORT H);
openButton.setLabel("Open" );
openButton.addMouseListene r(new java.awt.event.MouseAdapte r() {
public void mouseClicked(java.awt.even t.MouseEve nt evt) {
openButtonMouseClicked(evt );
}
});
jPanel3.add(openButton);
newButton.setLabel("New");
newButton.addActionListene r(new java.awt.event.ActionListe ner() {
public void actionPerformed(java.awt.e vent.Actio nEvent evt) {
newButtonActionPerformed(e vt);
}
});
jPanel3.add(newButton);
exitButton.setLabel("Exit" );
exitButton.addMouseListene r(new java.awt.event.MouseAdapte r() {
public void mouseClicked(java.awt.even t.MouseEve nt evt) {
exitButtonMouseClicked(evt );
}
});
jPanel3.add(exitButton);
getContentPane().add(jPane l3, java.awt.BorderLayout.SOUT H);
pack();
}
private void exitButtonMouseClicked(jav a.awt.even t.MouseEve nt evt) {
System.exit (0) ;
}
private void openButtonMouseClicked(jav a.awt.even t.MouseEve nt evt) {
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(thi s);
if(returnVal == JFileChooser.APPROVE_OPTIO N) {
java.io.File f = chooser.getSelectedFile();
System.out.println("You chose to open this file: " + f.getAbsolutePath() );
try {
BufferedReader br = new BufferedReader ( new java.io.FileReader(f) );
String tempLine = null ;
jTextArea1.setText(null);
while( ( tempLine = br.readLine() ) != null ) { jTextArea1.append( tempLine + "\n" ); }
}
catch ( java.io.FileNotFoundExcept ion e ) {
e.printStackTrace();
System.exit (-1);
}
catch ( java.io.IOException e ) {
e.printStackTrace();
System.exit (-1);
}
}
}
private void newButtonActionPerformed(j ava.awt.ev ent.Action Event evt) {
jTextArea1.setText(null);
}
/** Exit the Application */
private void exitForm(java.awt.event.Wi ndowEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
new FileViewer().show();
}
}
import java.io.BufferedReader;
import javax.swing.JFileChooser;
public class FileViewer extends javax.swing.JFrame {
private javax.swing.JButton exitButton;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JButton newButton;
private javax.swing.JButton openButton;
public FileViewer() {
super("File Viewer");
initComponents();
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jPanel2 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
openButton = new javax.swing.JButton();
newButton = new javax.swing.JButton();
exitButton = new javax.swing.JButton();
addWindowListener(new java.awt.event.WindowAdapt
public void windowClosing(java.awt.eve
exitForm(evt);
}
});
jTextArea1.setPreferredSiz
jScrollPane1.setViewportVi
jPanel1.add(jScrollPane1);
getContentPane().add(jPane
getContentPane().add(jPane
openButton.setLabel("Open"
openButton.addMouseListene
public void mouseClicked(java.awt.even
openButtonMouseClicked(evt
}
});
jPanel3.add(openButton);
newButton.setLabel("New");
newButton.addActionListene
public void actionPerformed(java.awt.e
newButtonActionPerformed(e
}
});
jPanel3.add(newButton);
exitButton.setLabel("Exit"
exitButton.addMouseListene
public void mouseClicked(java.awt.even
exitButtonMouseClicked(evt
}
});
jPanel3.add(exitButton);
getContentPane().add(jPane
pack();
}
private void exitButtonMouseClicked(jav
System.exit (0) ;
}
private void openButtonMouseClicked(jav
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(thi
if(returnVal == JFileChooser.APPROVE_OPTIO
java.io.File f = chooser.getSelectedFile();
System.out.println("You chose to open this file: " + f.getAbsolutePath() );
try {
BufferedReader br = new BufferedReader ( new java.io.FileReader(f) );
String tempLine = null ;
jTextArea1.setText(null);
while( ( tempLine = br.readLine() ) != null ) { jTextArea1.append( tempLine + "\n" ); }
}
catch ( java.io.FileNotFoundExcept
e.printStackTrace();
System.exit (-1);
}
catch ( java.io.IOException e ) {
e.printStackTrace();
System.exit (-1);
}
}
}
private void newButtonActionPerformed(j
jTextArea1.setText(null);
}
/** Exit the Application */
private void exitForm(java.awt.event.Wi
System.exit(0);
}
public static void main(String args[]) {
new FileViewer().show();
}
}
ASKER
thanks for all the inputs, I apologize for being late. The GetFiles and Add Items answered my questions.
:-)
import javax.swing.*;
import java.io.*;
/*
* Created on Aug 10, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author Sudhakar
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class AddFiles extends JFrame {
JComboBox combo=new JComboBox();
public AddFiles()
{
init();
}
public void init()
{
addFiles(combo,"c:/");
JPanel pane=new JPanel();
pane.add(combo);
this.getContentPane().add(
}
public static void main(String s[])
{
AddFiles files=new AddFiles();
files.setSize( 400,300);
files.show() ;
}
public void addFiles(JComboBox _combo,String file)
{
File dir = new File(file);
String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a directory
} else {
for (int i=0; i<children.length; i++) {
// Get filename of file or directory
String filename = children[i];
_combo.addItem(filename );
}
}
}
}