Use FileChannels and ByteBuffers to Store Patterns : Serialization « Regular Expressions « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Regular Expressions » SerializationScreenshots 
Use FileChannels and ByteBuffers to Store Patterns

//Example File
/*       
#Email validator that adheres directly to the specification
#for email address naming. It allows for everything from
#ipaddress and country-code domains to very rare characters
#in the username.
email=^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-
9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-
9]{1,3})(\]?)$

#Matches UK postcodes according to the following rules 1. LN NLL
#eg N1 1AA 2. LLN NLL eg SW4 0QL 3. LNN NLL eg M23 4PJ 4. LLNN NLL
#eg WS14 0JT 5. LLNL NLL eg SW1N 4TB 6. LNL NLL eg W1C 8LQ Thanks
#to Simon Bell for informin ...
zip=^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$

#This regular expression matches dates of the form XX/XX/YYYY
#where XX can be 1 or 2 digits long and YYYY is always 4
#digits long.
dates=^\d{1,2}\/\d{1,2}\/\d{4}$

*/

import java.util.Properties;
import java.util.regex.*;
import java.util.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.logging.Logger;

public class RegexProperties extends Properties {
  private static Logger log = Logger.getAnonymousLogger();

  public void load(String inStreamthrows IOException,
      PatternSyntaxException {
    load(new FileInputStream(inStream));
  }

  public void load(FileInputStream inStreamthrows IOException,
      PatternSyntaxException {
    FileChannel fc = inStream.getChannel();

    ByteBuffer bb = ByteBuffer.allocate((intfc.size());
    fc.read(bb);
    bb.flip();
    String fileContent = new String(bb.array());

    Pattern pattern = Pattern.compile("^(.*)$", Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(fileContent);

    while (matcher.find()) {
      String line = matcher.group(1);
      if (line != null && !"".equals(line.trim())
          && !line.startsWith("#"&& !line.startsWith("!")) {
        String keyValue[] null;
        if (line.indexOf("="0)
          keyValue = line.split("="2);
        else
          keyValue = line.split(":"2);

        if (keyValue != null) {
          super.put(keyValue[0].trim(), keyValue[1]);
        }
      }
    }
    fc = null;
    bb = null;
  }

  public void store(FileOutputStream out, String header)
      throws UnsupportedOperationException {
    throw new UnsupportedOperationException("unsupported for this class");
  }

  public void putAll(Map t) {
    throw new UnsupportedOperationException("unsupported for this class");
  }
}
           
       
Related examples in the same category
1. Using a Regular Expression to Filter Lines from a Reader
2. Reading Lines from a String Using a Regular Expression
3. Apply Regular Expressions on the contents of a file
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.