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. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net 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
w_w___w___.__j___a_v___a2___s.c___o_m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.