public static void randomSelection ( File input , int expected_number , boolean noTest ) throws IOException { BitSet biset = new BitSet ( ) ; Random random = new java . util . Random ( ) ; // find the number of lines in the original file int maxNumLines = 0 ; BufferedReader reader = new BufferedReader ( new FileReader ( input ) ) ; while ( ( reader . readLine ( ) ) != null ) { maxNumLines ++ ; } System . out . println ( "Original file : " + maxNumLines + " lines" ) ; boolean dumpAll = maxNumLines <= expected_number ; int added = 0 ; while ( added <= expected_number ) { int randomInt = random . nextInt ( maxNumLines + 1 ) ; if ( biset . get ( randomInt ) == false ) { biset . set ( randomInt , true ) ; added ++ ; } } // read the original raw file and generate a subset File output = new File ( input . getParentFile ( ) , input . getName ( ) + "_" + expected_number ) ; // put the rest in a file which we can use for eval File eval = new File ( input . getParentFile ( ) , input . getName ( ) + "_" + expected_number + ".test" ) ; reader . close ( ) ; reader = new BufferedReader ( new FileReader ( input ) ) ; BufferedWriter writer = new BufferedWriter ( new FileWriter ( output ) ) ; BufferedWriter writer2 = null ; if ( noTest == false ) writer2 = new BufferedWriter ( new FileWriter ( eval ) ) ; int count = 0 ; int kept = 0 ; String line = null ; while ( ( line = reader . readLine ( ) ) != null ) { if ( biset . get ( count ) || dumpAll ) { writer . append ( line + "\n" ) ; kept ++ ; } else if ( noTest == false ) { writer2 . append ( line + "\n" ) ; } count ++ ; } System . out . println ( "New file : " + kept + " lines" ) ; if ( noTest == false ) writer2 . close ( ) ; writer . close ( ) ; reader . close ( ) ; }
Generates a random sample of lines from an input file and stores the selection in a file named like the original but with the suffix _ + number of lines .