Guest User

Untitled

a guest
Jan 22nd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. public class FileParser {
  10.  
  11. /**
  12. *
  13. * @param args
  14. */
  15. public static void main(String[] args) {
  16. Pattern pattern = Pattern.compile(
  17. "(0[1-9]|1[0-2])/(0[1-9]|[1-2][0-9]|30|31)/([0-9]{4})",
  18. Pattern.CASE_INSENSITIVE);
  19. File file = new File("corpusCFPs-set1.txt");
  20. String fileContent = readFileAsString(file.getAbsolutePath());
  21.  
  22. Matcher match = pattern.matcher(fileContent);
  23. while (match.find()) {
  24. System.out.println("extrait : " + match.group());
  25. }
  26. }
  27. /**
  28. *
  29. * @param filePath
  30. * @return
  31. */
  32. private static String readFileAsString(String filePath) {
  33. byte[] buffer = new byte[(int) new File(filePath).length()];
  34. BufferedInputStream f = null;
  35.  
  36. try {
  37. f = new BufferedInputStream(new FileInputStream(filePath));
  38. } catch (FileNotFoundException e) {
  39. // TODO Auto-generated catch block
  40. e.printStackTrace();
  41. }
  42. try {
  43. f.read(buffer);
  44. } catch (IOException e) {
  45. // TODO Auto-generated catch block
  46. e.printStackTrace();
  47. }
  48. if (f != null)
  49. try {
  50. f.close();
  51. } catch (IOException ignored) {
  52. }
  53.  
  54. return new String(buffer);
  55. }
  56. }
Add Comment
Please, Sign In to add comment