princennamdi

file processor

Sep 28th, 2012
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.util.Arrays;
  4.  
  5.  
  6. class fyle implements Iterable<String>{
  7. private BufferedReader reader;
  8. public fyle(String path) throws Exception{
  9. reader = new BufferedReader(new FileReader(path));}
  10.  
  11. public void close() throws IOException{reader.close();}
  12.  
  13. public Iterator<String> iterator() {return new FileIterator();}
  14.  
  15. private class FileIterator implements Iterator<String>{
  16. private String currentLine;
  17.  
  18. public boolean hasNext() {
  19. try{currentLine = reader.readLine();
  20. }catch(Exception E){
  21. currentLine=null;
  22. E.printStackTrace();}
  23. return currentLine != null;}
  24.  
  25. public String next() {
  26. return currentLine;}
  27.  
  28. public void remove() {}
  29. }}
  30.  
  31.  public class fileClass {
  32. public static int n, position=0;
  33. public fileClass(String path) throws Exception{
  34. fyle f= new fyle(path);
  35. for(String j:f){n+=1;}
  36.  
  37. int[] k=new int[5];
  38.  
  39. for (int i=0; i<5; i++){ k[i] = 1+((int)(Math.random() * (n)));}
  40. Arrays.sort(k);
  41.  
  42. fyle f2= new fyle(path);
  43.  
  44. for(String j2:f2){
  45. position++;
  46. for(int i=0;i<5;i++){
  47. if(position== k[i]){
  48. System.out.println("Random Line Number:" + k[i] + "  " + j2);
  49. }}}}
  50.  
  51. public static void main(String[] args) throws Exception{
  52. try{
  53. fileClass _new=new fileClass(args[0]);
  54. }catch(Exception e){
  55. System.out.println("Error :p");
  56. }}}
Advertisement
Add Comment
Please, Sign In to add comment