Thu Jun 28, 2007 5:26 am
import java.io.*;
import java.io.File.*;
import java.util.*;
import javax.swing.JFileChooser;
try
{
JFileChooser loadEmp = new JFileChooser();//new dialog
File selectedFile;//needed*
BufferedReader in;//needed*
FileReader reader = null;//needed*,look these three up for further info
//opens dialog if button clicked
if (loadEmp.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
{
//gets file from dialog
selectedFile = loadEmp.getSelectedFile();
//makes sure files can be processed before proceeding
if (selectedFile.canRead() && selectedFile.exists())
{
reader = new FileReader(selectedFile);
}
}
in = new BufferedReader(reader);
//inputLine recieves file text
String inputLine = in.readLine();
int LineNumber = 0;
while(inputLine!=null)
{
//LineNumber isn't needed, but it adds a line count on the left, nice
LineNumber++;
StringTokenizer tokenizer = new StringTokenizer(inputLine);
//displays text file
jTxtAMain.append(LineNumber+": "+inputLine+"\n");
//next line in File opened
inputLine = in.readLine();
}
//close stream, files stops loading
in.close();
}
//catches input/output exceptions and all subclasses exceptions
catch(IOException ex)
{
jTxtAMain.append("Error Processing File:\n" + ex.getMessage()+"\n");
}
//catches nullpointer exception, file not found
catch(NullPointerException ex)
{
jTxtAMain.append("Open File Cancelled:\n" + ex.getMessage()+"\n");
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.