command prompt - Reading from a text file in java is returning some garbage value -


i'm performing commands through command prompt , storing values in text file.

 wmic logicaldisk drivetype=3 deviceid > drive.txt 

now want read string stored in text file java file. when try this:

            try {             file file = new file("drive.txt");             filereader reader = new filereader(file);             bufferedreader in = new bufferedreader(reader);             int i=0;             while ((string[i] = in.readline()) != null) {                 system.out.println(string[i]);                     ++i;              }             in.close();           } catch (ioexception e) {             e.printstacktrace();           } 

i output follows:

 ÿþd[]e[]v[]i[]c[]e[] 

how avoid this?

  while ((string[i] = in.readline()) != null) {                 system.out.println(string[2]);             } 

over there missing i++;

however advise use structure: use arraylist instead of array, since allows have self-resizing structure, instead in while use method ready(); bufferedread in order check end document, @ end it's display elements in string arraylist.

arraylist<string> string = new arraylist<string>();     try {          file file = new file("drive.txt");         bufferedreader entrada;         entrada = new bufferedreader(new filereader(file));          entrada.readline();         while (entrada.ready()) {              string.add(entrada.readline());         }         entrada.close();     } catch (ioexception e) {         e.printstacktrace();     }     (string elements : string) {         system.out.println(elements);     } 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -