My problem is very simple and yet I can't figure out how to solve it. I have text files in a folder: "C:/aaa/bbb/ccc/ddd/test.txt" And excel files in a folder within the text files folder: "C:/aaa/bbb/ccc/ddd/excelFiles/test.xls" Both text and excel files have the same name.
I would like to be able to access the path of those excel files.
What I did is:
this.file.getParent()+"\\"+"excelFiles"+"\\"+file.getName().substring(0, fileName.indexOf('.'))+".xls"
I get a "String index out of range" error. Thank you for your help :)
You might want to try this ->
String dynamicExcelFileName = file.getName().substring(0, fileName.indexOf('.'))
into a variable and use it in the path for accessing the excel file.
this way you get to be extra sure to check if the path is properly captured in variable or not and thus less chances of getting index out of range error.
plus the code is more readable
If I understand your question, one option is to use
File.getCanonicalPath()
like,This could be achieved quite easily, even without using existing libraries like FileUtils.
These three method can create the corresponding Excel
File
object for your text objectIf you use them like this:
.. it will print:
this.file.getParent()+"\"+"excelFiles"+"\"+file.getName().substring(0, this.file.getName().indexOf('.'))+".xls"
Looking at your snippet, I can see that you're accessing the file's name in two different ways:
and
Are you sure that
fileName
is not empty when you try to determine the index of the dot?