string - Java parsing and changing CD -


So I thought using the CMD command CD that the Java program inside of you returns the source folder of the Java program. Now I should be able to parse the string and capture the user's username. What do I mean here

If the code below works, which produces: C: \ Users \ Bob \ Desktop \ newfolder \ JavaProgs \ prog1 ... How can I parse a dynamic variable, Like the user name Bob above, string?

  Public static zero cdir () throws invalid command exception {try {string command = "cd"; String s = get_commandline_results (command); String [] temp1 = s.split ("\ n"); LinkedList & LT; String & gt; Temp2 = new linked list & lt; String & gt; (Arrays.asList (temp1)); Println (temp2.get (0)); FilePath = temp2.get (0); } Hold (exception e) {e.printStackTrace (); }} Public static string throws the get_commandline_results (string CMD) IOException, blocked exceptions, illegal commissions exception {if (! Authorized quand (!) (New illegal comma exception); } String result = ""; Final process p = runtime.gettime (). Exec (string format ("CMD / C% s", CMD)); Last process ResultReader stderr = New process ResultReader (p.getErrorStream (), "STDERR"); Last process ResultReader stdout = New process ResultReader (p.getInputStream (), "STDOUT"); Stderr.start (); Stdout.start (); Final integer turns out = P.waitFor (); If (exitValue == 0) {result = stdout.toString (); } And {result = stderr.toString (); } Return results; } Public Static Boolean Authorized Coin (String CMD) {if (CMD.AJLLS ("CD")) is true; return false; }  

How did I solve: System.getProperty ("user.name");

So if I want to print the username of the currently logged in user, then I do: System.out.println (System.getProperty ("user.name"));

:)


Comments