import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; import javax.swing.JFileChooser; import javax.swing.JOptionPane; /* * Created on Apr 26, 2006 * Demonstration of String handling via Regular Expressions */ public class BasicRegEx { /** * Standard entry for all Java programs * @param args */ public static void main(String[] args) { new BasicRegEx().start(); } private void start() { String filename = null; // without path spec, JVM assumes the file is in the project directory // JFileChooser jfc = new JFileChooser(); // starts in My Documents folder JFileChooser jfc = new JFileChooser("."); // starts in working folder (project folder) jfc.setDialogTitle("Please enter the name of a text file to read"); jfc.setFileFilter( new CustomFilter() ); int status; status = jfc.showDialog(null, "Press me!"); // "generic" dialog with customizable "approve" button // status = jfc.showOpenDialog(null); // has an "open" button // status = jfc.showSaveDialog(null); // has a "save" button if( status == JFileChooser.APPROVE_OPTION ) { // "Open" button was pressed File selected = jfc.getSelectedFile(); filename = selected.getName(); System.out.println(filename); } else { // Cancel was pressed System.exit(0); } // try reading or writing a file, based on the user's choice try { String contents = readTextFile( filename ); saveTextFile( "copy of " + filename, contents ); } catch( IOException e ) { // catch only IOExceptions during read or write System.out.println( "IO Error: " + e ); } catch( Exception e ) { // catch any and all other exceptions during read or write System.out.println( "General Error: " + e ); } // end try } // read a text file and create a roster from it private String readTextFile( String filename ) throws Exception { FileReader fr = new FileReader( filename ); Scanner reader = new Scanner( fr ); String contents = ""; // initially empty try { while( reader.hasNextLine() ) { // keep reading as long as there's something to read String line = reader.nextLine(); assert(0==1):"ERROR"; if( containsPattern(line ) ) System.out.println(line + " :contains Pattern"); contents += line + "\n"; } return contents; } finally { // guarantees that this code block will execute before exiting the method, // whether an exception was thrown or not. System.out.println("Closing file reader"); reader.close(); } } // save the roster to a text file private void saveTextFile(String filename, String contents) throws Exception { // attach to a File object (file or directory) File fileObject = new File( filename ); FileWriter fw = new FileWriter( fileObject, false ); // use 'false' to overwrite an existing file // FileWriter fw = new FileWriter( filename, true ); // alternate FileWriter construction PrintWriter pw = new PrintWriter(fw); // PrintWriter pw = new PrintWriter(filename); // alternate PrintWriter construction try { pw.print( contents ); } finally { pw.close(); // good behavior + it guarantees the data is flushed to the file } } private boolean containsPattern( String s) { if( s.matches(".*[0-9].*") ) // any occurrence of digits // if( s.matches("[a-zA-Z ]*") ) // any occurrence of letters or blanks // if( s.matches(".*[jJ]ava.*") ) // any occurrence of "Java" or "java" // if( s.matches( ".*[a-zA-Z ]+[0-9][0-9].*" )) // any letters or blank followed by 2 digits // if( s.matches( ".*[a-zA-Z ]{2}[0-9]{4}.*" )) // two letters or blank followed by 2 digits return true; else return false; } /* for( String s : myList ) { // replaces entire line containing a time sequence // If we want to replace just the time sequence and leave the remaining // components of the line in place, we have to use find() instead. s=s.replaceAll(".*[1-9]:[0-9][0-9]","time TBD"); System.out.println(s); } */ /** * Custom FileFilter * @author hornick * */ private class CustomFilter extends javax.swing.filechooser.FileFilter { @Override /* * sets what files are accepted */ public boolean accept(File file) { if( file.isDirectory() ) // directory return true; if( file.getName().toLowerCase().endsWith(".txt") ) // text file return true; return false; } @Override /* * the label of the filter */ public String getDescription(){ return ".txt files"; } } }