473,320 Members | 1,993 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Checking for a FileNotFoundException in a Seperate Method

Cleo Millet
This is just a simple program to check if a file exists or not, with the user input and the results 'File exits'/'File does not exist' all in the main method and the actual Exception catching being done in a separate method. I've got the basics of the doesFileExist method to catch any exceptions, but I can't figure out how to return that information to the main method so I can inform the user of the results.

Expand|Select|Wrap|Line Numbers
  1. import java.io.FileNotFoundException;
  2. import java.io.IOException;
  3. import java.io.RandomAccessFile;
  4. import java.util.Scanner;
  5.  
  6. public class FileApp {
  7.  
  8.     public static void main(String[] args) {
  9.  
  10.         Scanner sc = new Scanner(System.in);
  11.  
  12.         String filePath = doesFileExist(sc, "Enter the file's full path: ");
  13.  
  14.         if(!validFilePath) {
  15.             System.out.println("This File does not exist.");
  16.         }
  17.         else
  18.             System.out.println("This File exists.");
  19.  
  20.     } // End Main
  21.  
  22.  
  23.     public static String doesFileExist(Scanner sc, String prompt) {
  24.         RandomAccessFile file;
  25.         String filePath = null;
  26.         boolean validFilePath = false;
  27.  
  28.         while (!validFilePath) {
  29.             try {
  30.                 file = new RandomAccessFile(filePath, "p");
  31.                 validFilePath = true;                    
  32.             } // End Try Statement    
  33.  
  34.             catch (FileNotFoundException e) {
  35.             } // End Catch Statement 1
  36.  
  37.             catch (IOException e) {
  38.             } // End Catch Statement 2
  39.  
  40.      } // End doesFileExist Class
  41.         return filePath;
  42.  
  43. } // End Class FileApp
  44.  
  45. }
Nov 23 '11 #1
0 1243

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: normd | last post by:
I have a text file which contains VB.NET code. The VB.NET code is actually a Class that can contain code, which simply displays a message box or does something more complicated (i.e Excel/Word...
2
by: Ken Kast | last post by:
I have a class in which a specific event handler is passed in as a callback. I want to be able to determine at run time for a specific instance of the class whether or not the event handler has...
15
by: djtosh | last post by:
hi there hoping someone can help me out with this. im designing a code that is a basic database holding peoples details and i need to implement a method that searches by txt for a record by their...
7
by: sprash | last post by:
Newbie question: I'm trying to determine if a file physically exists regardless of the permissions on it Using File.Exists() returns false if it physically exists but the process does not...
2
by: Daniel =?iso-8859-1?Q?Lidstr=F6m?= | last post by:
Hi! I want to know if a certain member function is available in a COM object, before I try to call it. I'm using late binding and when I know the member exists, everything is fine. Here's the...
7
by: cbmeeks | last post by:
Hope I'm using the right terminology. Anyway, say I have a class like: class Animal { public double GetValues() {......} public void FilterBy(string text); {......}
7
by: Jeff | last post by:
I need a way to do the following and cannot seem to find a solution via google. 1. Have a method from the main app to get all open forms 2. Check each open form for a public method 3. If this...
6
by: Christopher | last post by:
Its been awhile and I am rusty. Can the constructor of my class call another method in the same class if that other method does not change any member data? I want to simply have a seperate...
6
by: minifish | last post by:
I'm calling a method that need to open up the af.sh file. Because the file does not exist, I get error: java.io.FileNotFoundException: \file\af.sh (The system cannot find the path specified) I...
1
by: monkey0525 | last post by:
As the title of the question states, my program is not able to store an integer from a seperate text file provided. For instance, instead of printing the desired output: "$45.00 17 2222 Chuck...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.