0

I have to do a project without the swing library.I have to browse over the files and was trying to do something like this:

fichero = new File(fichero.getAbsolutePath().concat("\\" + str));

where str is the new Directory that you want to access

Hope someone could help me. Thank you

Kara
  • 6,115
  • 16
  • 50
  • 57

2 Answers2

0

This

fichero = new File(fichero.getAbsolutePath() + "\\" + str);

or

fichero = new File(fichero.getAbsolutePath() + "/" + str);

or

fichero = new File(fichero.getAbsolutePath().concat("/").concat(str);

should work fine.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

You may use \\ or you may use a /. I suggest you use the latter as it is platform independent.

Also, use the constructor:

File(String parent, String child)  

The docs say:

Creates a new File instance from a parent pathname string and a child pathname string. If parent is null then the new File instance is created as if by invoking the single-argument File constructor on the given child pathname string.

Otherwise the parent pathname string is taken to denote a directory, and the child pathname string is taken to denote either a directory or a file. If the child pathname string is absolute then it is converted into a relative pathname in a system-dependent way. If parent is the empty string then the new File instance is created by converting child into an abstract pathname and resolving the result against a system-dependent default directory. Otherwise each pathname string is converted into an abstract pathname and the child abstract pathname is resolved against the parent.

So, your code should look like:

fichero = new File(fichero.getAbsolutePath(),str);  

NB: You may also use the File constructor that accepts a File and String as an argument, thus eliminating the call to getAbsolutePath()

An SO User
  • 24,612
  • 35
  • 133
  • 221