loops - How to sort and rearrange the file data in Java? -
i have txt file saved in format:
806pyznmnaw4h0wh8fywdq 0 0 0 0 0 1 0 bvu13gyouwhejpum2xjiqq 0 0 0 1 0 0 0 kduecqnk0mrjh9hcyhdkuw 1 0 1 0 0 0 0 806pyznmnaw4h0wh8fywdq 1 0 1 0 0 0 0 bvu13gyouwhejpum2xjiqq 0 1 1 1 1 0 0 tvct2yt3bllpu2crut0zjw 1 0 0 1 0 0 0 806pyznmnaw4h0wh8fywdq 1 1 1 0 0 0 0 9ify25dk87s5_u2ehk0_rg 0 0 0 1 0 0 0 bvu13gyouwhejpum2xjiqq 0 1 0 1 0 0 0 806pyznmnaw4h0wh8fywdq 1 0 0 0 0 0 0 zk0sniea8ju2ik0mw8ccrq 0 0 0 0 1 0 0 amuyug7kfwj7rcbt-elwrq 0 0 0 1 0 0 0
now need loop through input file , save sorted , rearranged output file based on first encoded value can occur more once in input file? e.g. in example file, have 806pyznmnaw4h0wh8fywdq
more once in random places.
this far tried: private static final string path = "newuserfeature.txt";
public static void main(string[] args) { list<string> list = new arraylist<string>(); list<string> uniquelist = new arraylist<string>(); try { scanner unique = new scanner(new filereader(path)); while (unique.hasnextline()) { string userunique = unique.nextline(); uniquelist.add(userunique); } iterator<string> = uniquelist.iterator(); while (it.hasnext()) { string user = it.next(); int = 0; int b = 0; int c = 0; int d = 0; int e = 0; int f = 0; int g = 0; iterator<string> it2 = list.iterator(); while (it2.hasnext()) { string[] y = it2.next().tostring().split(" "); if (user.equalsignorecase(y[0])) { int a1 = integer.parseint(y[1]); int b1 = integer.parseint(y[2]); int c1 = integer.parseint(y[3]); int d1 = integer.parseint(y[4]); int e1 = integer.parseint(y[5]); int f1 = integer.parseint(y[6]); int g1 = integer.parseint(y[7]); if (a1 == 1) { = a1; } if (b1 == 1) { b = b1; } if (c1 == 1) { c = c1; } if (d1 == 1) { d = d1; } if (e1 == 1) { e = e1; } if (f1 == 1) { f = f1; } } } system.out.println(user + " " + + " " + b + " " + c + " " + d + " " + e + " " + f + " " + g + "\n"); } } catch (filenotfoundexception e) { e.printstacktrace(); } }
if sorting lines first field happens string, can do:
path oldfile = paths.get("/path/to/my/file"); path newfile = paths.get("/path/to/my/newfile"); list<string> lines = files.readalllines(oldfile); collections.sort(lines); files.write(newfile, lines);
which:
1: load lines in list of strings,
2: sort string (which want @ end of day)
3: store lines in file
as doing string sorting first field, don't need break down each line columns.
Comments
Post a Comment