Results 1 to 15 of 15
  1. #1
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question Fully qualified file name is given as input. Look to see it exists or not

    Hi

    I would be of great help , if anyone can share their knowledge on how to do this in java

    The user enters the full path of the file , (which is considered for processing ) as the input .
    How to look to see if the file exists or not.

    Thank You in advance .

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default

    See the File class. It has a method to test if a file exists.

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

  4. #4
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question ? to give the fullpath and filename as input at the console

    Please provide me the statement to enter the filename and its path as the input at the console .

    Thanks in advance .

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default

    Read from System.in using a Scanner class method

  6. #6
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Read from System.in using a Scanner class method
    with this lines, i am able to give the full path and filename at the console .But how to test whether the given file exists or not .
    I need to check whether the file exists or not , and if it doesnot , i have to ask the user to enter the fully qualified filename again.?Pls help with the answer.Thank You

    Scanner console = new Scanner(System.in);
    System.out.println("Enter the fully qualified file name to be processed : ");
    String inFile = console.next();
    System.out.println("The file is located at :"+inFile);
    :
    :
    try {
    FileReader readin = new FileReader(inFile);
    Scanner in = new Scanner(readin);
    while (in.hasNextLine()){
    String input = in.nextLine();

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default

    I need to check whether the file exists or not
    Use the File class's exists method.

  8. #8
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Use the File class's exists method.
    Hi

    The exists class doesnot work here.
    i am using JDK1.6

    I tried

    inFile.<methodname>
    (their is not exists method in here???

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default

    The exists class doesnot work
    Sorry, I don't understand what does not work.
    Can you show the code with the problem?

    Do you know what I mean when I say the File class?

  10. #10
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Sorry, I don't understand what does not work.
    Can you show the code with the problem?

    Do you know what I mean when I say the File class?
    Sir,

    I am learning thru ur guidance .
    I wrote this code , seeing java sites to get the file with its full path as the console input.For which i wrote
    Scanner console = new Scanner(System.in);
    System.out.println("Enter the fully qualified file name to be processed : ");
    String inFile = console.next();
    System.out.println("The file is located at :"+inFile);

    It works fine. The string inFile contains the wholepath anf the filename in it.

    :
    :
    Then i started reading each line from the input file , for which i wrote the below lines ...and it works .
    try {
    FileReader readin = new FileReader(inFile);
    Scanner in = new Scanner(readin);
    while (in.hasNextLine()){
    String input = in.nextLine();

    I need to test whether the file exists before i start using the file.
    For which ,
    after this statement
    FileReader readin = new FileReader(inFile);
    I trying to use the exists method.But it doesnot come after the dot
    inFile.(........?

    File class , i am not using here .

    Your help will be greatly appreciated ...

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default

    I need to test whether the file exists before i start using the file.
    Let me repeat. Use the File class's exists method to determine if the file exits.

    Go to the API doc, find the File class and read about its Constructor and methods.

  12. #12
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question Exception in thread "main" java.lang.NullPointerException

    XML Code:
        Scanner console = new Scanner(System.in);
    	    System.out.println("Enter the fully qualified file name to be processed : ");
    	    String inFile = console.next();
    
    Scanner in=null;
    			   File readin = new File(inFile);
    			   System.out.println("The file is located at :"+readin);
    			  if(readin.exists()){
    				  System.out.println("Did u enter this loop");
    			  in  = new Scanner(readin);
    			  }else{
    				  System.out.println("Please enter the fully qualified filename again");
    			  }
    			   [COLOR="Red"] while (in.hasNextLine())//ERROR POINTS TO THIS LINE.{[/COLOR]			    	String input = in.nextLine();
    		
    //				for(String input ;(input = in. != null);){
    					recCount++;		
    					String[] column = input.split(",");
    					int columnCount = column.length;				
    					if(columnCount == 4){
    //						the String to int conversion happens here for each field of the record read.

    I need to repeat , till i get the correct filename.
    Can U correct me ...so that i can write and understand java coding

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default

    I need to repeat , till i get the correct filename.
    Put the code in a while loop and stay in the loop until you get a good name
    pseudo code:
    boolean gettingFilename = true;
    while(gettingFilename) {
    get name from user
    test if file exists
    if file exists, set gettingFilename false
    end while loop

  14. #14
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Smile Thank You Sir ..here is the code i wrote with all your help

    XML Code:
    	try {
    				File readin = null;
    				
    				  boolean gettingFilename = true;
    				  while(gettingFilename) {
    					  System.out.println("Enter the fully qualified file name to be processed : ");
    					    String inFile = scan.next();
    					     readin = new File(inFile);
    					 System.out.println("The file is located at :"+readin);
    					 if(readin.exists()){
    						  System.out.println("Did u enter this loop");
    						  gettingFilename = false;						 
    					 }else{
    							  System.out.println("Please enter the fully qualified filename again");
    						  }			 
    				  }

  15. #15
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Put the code in a while loop and stay in the loop until you get a good name
    pseudo code:
    Thank You So Much Sir.

    Your prompt replies where of great help for me in writing the piece of code .

    I am greatful for your kind help in time .

    Thanks Again .

Similar Threads

  1. Qualified S.C.J.P. 1.5
    By ankurrsmh in forum Java Certification
    Replies: 1
    Last Post: 05-24-2010, 05:12 PM
  2. Checking if file exists in a directory
    By achilles in forum New To Java
    Replies: 7
    Last Post: 08-10-2009, 03:43 AM
  3. Replies: 2
    Last Post: 04-07-2009, 07:25 AM
  4. How to check whether file is exists or not
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-05-2008, 11:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •