// RHYTM // using regular expressions to replace all the character and non/characters in a txt file // with a single character import java.io.*; import java.nio.*; import java.nio.channels.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Exercise2 { public static void main (String[] args) throws IOException { // Create an input stream and file channel // Using first argument as file name to read in FileInputStream fis = new FileInputStream(args[0]); FileChannel fc = fis.getChannel(); // Read the contents of a file into a ByteBuffer ByteBuffer bb = ByteBuffer.allocate((int)fc.size()); fc.read(bb); fc.close(); // Convert ByteBuffer to one long String String content = new String(bb.array()); //do something here //string to match String regex = "."; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(content); String output = m.replaceAll("/"); // Create an output stream and file channel to write out a report // (Also print out report to screen) FileOutputStream fos = new FileOutputStream(args[1]); FileChannel outfc = fos.getChannel(); // Convert content String into ByteBuffer and write out to file bb = ByteBuffer.wrap(output.getBytes()); outfc.write(bb); outfc.close(); } }