• Post Reply Bookmark Topic Watch Topic
  • New Topic

Advice on file directory needed.  RSS feed

 
Jerri Loh
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey.

I've got a programme that processes the files in a given directory. However, it can only run those files if the programme itself is saved in the same directory.

The error i am experiencing now is that once my programme runs the directory, it also generates a class file which also is processed by the programme (because it's saved in the same directory.)

This causes a jam that that aborts the whole operation.

Could I process the files without saving my java programme in the same folder?

I tried saving it another folder, but the programme wasn't able to access the files, though it could list the file names.




Thanks again!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of this, in line 4:

Try this:

So, leave off the +1, because you don't want the directory name to end with a backslash.
 
Jerri Loh
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I tried it. But it still threw the same IOException for my programme. its not able to access the files. Sorry but thank you really much!
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried running your code (changing it for my environment) and it worked. Which line of code is causing the IOException?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I missing something or can't you just use:
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suggestion: This has nothing to do with your problem (probably) but take a look at using File.separator or one of its variants rather than using "\\".
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerri Loh wrote:

What's wrong with using getParentFile()?
Or of course directly set the path as Wouter suggested.
 
Jerri Loh
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I totally get what you're saying and I've tried every one of your suggestions. Thank you for helping me shorten my code, but um. I am still facing the same problem. If i don't save the programme in the save folder as my files to be processed, the programme is not able to access the files. only the file names. period. and if i do save it in the same folder- bam. the programme runs well but takes in the class file generated in the folder and the operation is aborted.

I am thinking whether it has gotta do with my datastructures.


 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
list() returns the names of the files (only), not the complete path to the file. Therefore when you open a file for reading, it only works if your current directory is the directory where the file lives. The solution is to supply the whole paths. One easy way: make "dir" an array of File, not String, and then use the listFiles() method rather than the list() method. FileReader can open a File object just as well as it can open a file using the name as a String, but the File will contain the complete path.
 
Jerri Loh
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so so much! You're a genius!!! Thank you all!!! Very grateful.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!