Running AWK from Java to create a file and reading it with BufferedReader -


i trying create csv file plain text file sorted, , i'm trying read bufferedreader. question if runs simultaneously or if awk part first , reads..

the awk file creation part:

string uniquesubscriberscommand = "cat " + originalfile +                 " | awk '$1~/^[0-9]*$/ {print $0}' | sort -k 1 | awk '{print $1}' | uniq >> " +                 uniquefile; try {     runtime.getruntime().exec( new string[]{"/bin/sh", "-c", uniquesubscriberscommand} ); } catch ( ioexception e ) {      logger.error( "error during unique subscriber determination" ); } 

the reading part, right after creation part:

fileinputstream uniquefis = new fileinputstream( uniquefile ); bufferedreader brunique = new bufferedreader( new inputstreamreader( uniquefis ) ); while ( ( subscriberid = brunique.readline() ) != null )             {                 // stuff             } 

i interested in knowing if java can read file created if put thread sleep right after running awk command create let's 10 second gap between creation , reading.

thanks advice!

try following - rough idea how can done. haven't taken great care close unclosed streams , such. make sure that. , may read more efficiently using buffer random access file, haven't taken care of too. sorry that.

note: following example ssccp kind of example test reading file can altered outside , update in console. if works, or think worthy, can improve according use case have mentioned, 1 thread writing file , other read.

        import java.io.ioexception;         import java.io.randomaccessfile;         import java.nio.channels.filechannel;          public class readfromfile {              /**              * @param args              * @throws ioexception              */             public static void main(string[] args) throws ioexception {                  runnable runnable = new runnable() {                      //it's choice how close channel                     filechannel in = null;                      @override                     public void run() {                         long lastchannelpos = 0l;                         try {                             /*                              *i assume run forever                              *if don't want run forever                              *put condition on here in while loop.                             */                             while (true) {                                 randomaccessfile raf = new randomaccessfile(                                         "your_file_loc", "r");                                 raf.seek(lastchannelpos);                                 int c = 0;                                 stringbuffer sb = new stringbuffer();                                 while ((c = raf.read()) != -1) {                                     sb.append((char) c);                                 }                                 in = raf.getchannel();                                 lastchannelpos = in.position();                                  if (!sb.tostring().trim().isempty()) {                                     //you can print or use output wish                                     //for simplicity i'm printing                                      system.out.print(sb.tostring().trim());                                 }                                 thread.sleep(1000);                                 raf.close();                             }                         } catch (exception e) {                             e.printstacktrace();                         }                     }                  };                  thread th = new thread(runnable);                 th.start();              } 

hope helps.


Comments