• Post Reply Bookmark Topic Watch Topic
  • New Topic

File Reader not letting to go to next statement even after compiling and running  RSS feed

 
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

Its not reading after this line FileReader fr1=new FileReader(existingDirFile); please assist where I am going wrong.

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say "not reading" I assume you mean "not executing." I'm curious as to why you would think that.

I'm also curious as to why, after nearly 1,000 posts here, you're still ignoring exceptions, rather than at least printing a stack trace.
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:When you say "not reading" I assume you mean "not executing." I'm curious as to why you would think that.

I'm also curious as to why, after nearly 1,000 posts here, you're still ignoring exceptions, rather than at least printing a stack trace.


Thank you Jeff , going forward i'l write down the exception statements.
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



I am getting file not found exception? FileReader refrence variable should create it right??
 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FileReader will try to "read",it won't create.
the [tt]existingDir1.mkdir(); [//tt] returns a boolean to indicate if the file was created. Check that value to see why the file is not being created.
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:FileReader will try to "read",it won't create.
the [tt]existingDir1.mkdir(); [//tt] returns a boolean to indicate if the file was created. Check that value to see why the file is not being created.


existingDir1.mkdir(); is returning false, but the directory is being created???
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I understood what was the issue, the file was not existing and I was simply trying to read the file that doesnot exist


But one query when I am trying to print


why it is giving the value as false even after a directory is being created?

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:
why it is giving the value as false even after a directory is being created?


If it's returning false, then it did not create the directory. This is a fact. You're assuming it created the directory, but it did not.
 
Vishal Hegde
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Vishal Hegde wrote:
why it is giving the value as false even after a directory is being created?


If it's returning false, then it did not create the directory. This is a fact. You're assuming it created the directory, but it did not.



No, I am not assuming I Just tried the code and checked it.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:
Jeff Verdegan wrote:
Vishal Hegde wrote:
why it is giving the value as false even after a directory is being created?


If it's returning false, then it did not create the directory. This is a fact. You're assuming it created the directory, but it did not.



No, I am not assuming I Just tried the code and checked it.


Checked what?

My guess is that you saw the method return false, saw that the directory exists, and jumped to the wrong conclusion. The method returning false and the directory existing after the method call is a perfectly valid scenario. You just might be overlooking something obvious.

And separate from that little brain teaser is the possibility that it's not the same directory. Did you look at the canonical path of the one you're attempting to create in your Java code to confirm that it's the same as where you're seeing the directory on the file system?
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could the directory already exist? I've never tried it, but I would think that would return a 'false' since it didn't create it, but it might look like it did, since when you go check the dir is there.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:No, I am not assuming


Yes, you are. You're assuming multiple things, and at least one of them is false.

We all make assumptions all day long, every day, in everything that we do. There's nothing inherently wrong with that. If we didn't do it, we couldn't function. However, when things don't go as expected, we need to be aware of what assumptions we've made and reevaluate them to see which ones might be incorrect. This becomes blatantly obvious in programming (especially when people like me point it out ) but it also applies in every day life.

"Why isn't he here?!!? I left him a voice mail and told him to meet me at McDonald's at 9:00, but he didn't bother to come. What a jerk!"

  • I'm assuming he got my voicemail. But maybe he hasn't heard it yet.
  • I'm assuming he knew I meant the McDonald's next to my office, rather than the one a block from his house.
  • I'm assuming he knows I meant 9:00 a.m., not 9:00 p.m.
  • I'm assuming he deliberately chose not to show up. But maybe he got stuck in traffic. Or maybe something more urgent came up. Or maybe he got lost. etc.



  • In the case of this issue with the directory, you're making at least 2 potentially faulty assumptions:

  • You're assuming the directory you asked Java to create is the same directory as the one you're seeing on the file system.
  • You're assuming that a false result from mkdir() means the directory doesn't exist after the call.

  •  
    Vishal Hegde
    Ranch Hand
    Posts: 1078
    Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Jeff , I understood where I was going wrong and enjoying the process of asking questions over here.What I was doing was on line 7 i was making dir.mkdir() which at that point is creating directory and on line 8 I am simply using sysout to print true or false as the directory is already created on line 7.. Line 8 will obiviously return false.Below is the code



    Also consider this second code.I have removed the 7th line from the previous code and this time System.out.println will return true as dir.mkdir() will create directory at that line
     
    Maneesh Godbole
    Bartender
    Posts: 11445
    18
    Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Vishal Hegde wrote:Thank you Jeff , I understood where I was going wrong and enjoying the process of asking questions over here.What I was doing was on line 7 i was making dir.mkdir() which at that point is creating directory and on line 8 I am simply using sysout to print true or false as the directory is already created on line 7.. Line 8 will obiviously return false.Below is the code


    You can always use the File#exists() to check if the...well file exists
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!