-2

I have this code witch detect file on usb, I want to add the name of the files to array list of strings, how can I continue?

                final String CP = "cp ";
                final String DEST = " /home/user/Desktop";
                final String TERMINAL_COMMAND = "ls /media/user/";

                String USB_UUID = null;
                //System.out.println(TERMINAL_COMMAND);
                try {
                    Process p = Runtime.getRuntime().exec( TERMINAL_COMMAND );
                    BufferedReader in = new BufferedReader(
                            new InputStreamReader(p.getInputStream()) );

                    while ((USB_UUID = in.readLine()) != null) {

                        //System.out.println(USB_UUID);
                        final String FILETOSTART = "/media/user/"+USB_UUID;


                    }
                    in.close();
                } 
Reto
  • 1,305
  • 1
  • 18
  • 32
  • The while(....){} is actually printing each file in the usb? – GOXR3PLUS May 15 '16 at 11:47
  • I don't really see your question. Is it that you cannot change this code for some reason and you are looking to see how to extract the filenames? I also don't see at this point where DEST and CP are. By looking at your code you also have a FILETOSTART variable. Also the code doesn't follow CamelCase notation. Could you provide further explanation please? – Joao Esperancinha May 15 '16 at 11:50
  • is the question just "how to list files in a directory"? if so, https://docs.oracle.com/javase/tutorial/essential/io/dirs.html#listdir – Joni May 15 '16 at 11:55
  • The goal is to add all the files names that is in the path to a String list, I have this line File[] listOfFiles = folder.listFiles(); , but I cants parse the list as strings – Stevie Sommers May 15 '16 at 12:16
  • God, if you don't write properly you won't get any help. `but I cants parse the list as strings` What does that mean ? Why not ? Post something we can't read your mind. – UDKOX May 15 '16 at 15:14

2 Answers2

0

Do you think this could be helpful? It's using the io.File class, but I think .nio could also be used.

import java.io.File;
import java.io.FileNotFoundException;
File f = /media/user/;
String[] fsl = f.list();
for(Stinrg fs : fsl) {
    System.out.println(fs);
}
dur
  • 15,689
  • 25
  • 79
  • 125
loadP
  • 404
  • 1
  • 4
  • 15
0

how can I continue? Depends, what do you want ? If you want a list of files on a directory, your approach is pretty bad imo. Also, your question has been answered several times, not only in this forum, but all over the internet. Is it that hard ?

Google : java list files in directory

Results: 1, 2, 3 ..

Did you try all those before asking ? We are not a googling service / code-writing service, try it urself first.

Community
  • 1
  • 1
UDKOX
  • 738
  • 3
  • 15