14.07.2013 Views

Contents - Cultural View

Contents - Cultural View

Contents - Cultural View

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

New I/O 235<br />

// Pattern used to parse lines<br />

private static Pattern linePattern = Pattern.compile(".*\r?\n");<br />

// The input pattern that we're looking for<br />

private static Pattern pattern;<br />

// Compile the pattern from the command line<br />

private static void compile(String pat) {<br />

}<br />

applying<br />

try {<br />

pattern = Pattern.compile(pat);<br />

} catch (PatternSyntaxException x) {<br />

}<br />

System.err.println(x.getMessage());<br />

System.exit(1);<br />

// Use the linePattern to break the given CharBuffer into lines,<br />

// the input pattern to each line to see if we have a match<br />

private static void grep(File f, CharBuffer cb) {<br />

}<br />

Matcher lm = linePattern.matcher(cb); // Line matcher<br />

Matcher pm = null; // Pattern matcher<br />

int lines = 0;<br />

while (lm.find()) {<br />

}<br />

lines++;<br />

CharSequence cs = lm.group(); // The current line<br />

if (pm == null)<br />

else<br />

pm = pattern.matcher(cs);<br />

pm.reset(cs);<br />

if (pm.find())<br />

System.out.print(f + ":" + lines + ":" + cs);<br />

if (lm.end() == cb.limit())<br />

break;<br />

// Search for occurrences of the input pattern in the given file<br />

private static void grep(File f) throws IOException {<br />

// Open the file and then get a channel from the stream<br />

FileInputStream fis = new FileInputStream(f);<br />

FileChannel fc = fis.getChannel();<br />

// Get the file's size and then map it into memory<br />

int sz = (int)fc.size();<br />

MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!