|
Reading Data from a file
I am working on a project, creating a Notepad..
I want to read the text file from the disk...
My problem is that the forth coming code reads only some of the text files and throws exception while reading some files...
My second problem is that the forth coming code reads the text files by leaving the first line of the text files
import javax.swing.filechooser.FileFilter;
public class FileOpenClass extends Component {
MyNotepadWindow openAction;
ActionClass ActionClass;
public FileOpenClass(MyNotepadWindow openAction,ActionClass ActionClass)
{
this.ActionClass = ActionClass;
this.openAction = openAction;
}
public FileOpenClass(MyNotepadWindow openAction, String action,ActionClass ActionClass) {
this(openAction,ActionClass);
String selectedFile;
String[] textDocuments = new String[] {"ini","bat","txt"};
JFileChooser openChooser = new JFileChooser();
openChooser.setMultiSelectionEnabled(false);
BufferedReader br;
try
{
openChooser.addChoosableFileFilter(new DisplayFilters(textDocuments,
"Text Documents (*.ini,*.bat,*.txt)"));
int option = openChooser.showOpenDialog(new JFrame());
if(option == JFileChooser.APPROVE_OPTION) {
if(openChooser.getSelectedFile() !=null) {
selectedFile = openChooser.getSelectedFile().getName();
br = new BufferedReader(new FileReader(selectedFile));
openAction.notes.setText(" ");
while((br.readLine()) != -1) {
openAction.notes.append(br.readLine()+"\n");
}
openAction.setTitle(selectedFile + " - MyNotepad");
if(ActionClass.statusBar.isVisible()) {
ActionClass.statusBar.setText("you selected " + selectedFile);
}
}
else if (option == JFileChooser.CANCEL_OPTION) {
if(ActionClass.statusBar.isVisible()) {
ActionClass.statusBar.setText("you didn't select any file");
}
}
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
|