import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.EOFException; import javax.swing.JFileChooser; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; import java.io.StreamTokenizer; import javax.swing.JOptionPane; public class BasicFile { File f; JFileChooser select; File f2 = new File(".", "File Backup"); JFileChooser choose; public BasicFile() { choose = new JFileChooser("."); } public void selectFile() { int status = choose.showOpenDialog(null); try { if (status != JFileChooser.APPROVE_OPTION) throw new IOException(); f = choose.getSelectedFile(); if (!f.exists()) throw new FileNotFoundException(); } catch(FileNotFoundException e) { display(e.toString(), "File not found ...."); } catch(IOException e) { display(e.toString(), "Approve option was not selected"); } } void display(String msg, String s) { JOptionPane.showMessageDialog(null, msg, s, JOptionPane.ERROR_MESSAGE); } void backupFile() throws FileNotFoundException { DataInputStream in = null; DataOutputStream out = null; try { in = new DataInputStream(new FileInputStream(f)); out = new DataOutputStream(new FileOutputStream(f2)); try { while (true) { byte data = in.readByte(); out.writeByte(data); } } catch(EOFException e) { JOptionPane.showMessageDialog(null, "Success!!!", "Backup Complete!", JOptionPane.INFORMATION_MESSAGE); } catch (IOException e) { JOptionPane.showMessageDialog(null, "File Not Found ", "Error", JOptionPane.INFORMATION_MESSAGE); } } finally { try { in.close(); out.close(); } catch(Exception e) { display(e.toString(), "Error"); } } } boolean exists() { return f.exists(); } public String toString() { return f.getName() +"\n" +f.getAbsolutePath() +"\n" +f.length() +" bytes"; } public String wordCount() { try { int wordCount = 0, numberCount = 0, lineCount = 1, characterCount = 0, totalWords = 0; String c = " "; FileReader r = new FileReader(f); StreamTokenizer t = new StreamTokenizer(r); t.resetSyntax(); t.whitespaceChars(0, ' '); t.wordChars('a','z'); t.wordChars('A','Z'); t.wordChars('0','9'); t.eolIsSignificant(true); while(t.nextToken() != StreamTokenizer.TT_EOF) { switch(t.ttype) { case StreamTokenizer.TT_NUMBER: numberCount++; break; case StreamTokenizer.TT_WORD: characterCount += t.sval.length(); wordCount++; break; case StreamTokenizer.TT_EOL: lineCount++; break; case StreamTokenizer.TT_EOF: break; default: } } r.close(); totalWords = numberCount + wordCount; return f.getName() + " has " + lineCount + " line(s), " + totalWords + " word(s), " + characterCount + " characters. "; } catch(IOException e) { display(e.toString(), "Error"); } return " "; } public String lineSearch(String input) throws Exception { LineNumberReader lnr = new LineNumberReader(new FileReader(f)); String line = null; String msg = ""; // read lines while ((line = lnr.readLine()) != null) { if (line.toUpperCase().contains(input.toUpperCase())) { msg += lnr.getLineNumber() + ": " + line + "\n"; } } return msg; } }