Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File exists?

Posted on 2002-04-13
2
Medium Priority
?
260 Views
Last Modified: 2010-03-31
What is the easiest way within my JAVA program to find out if a file (myFile.txt) exists in the current directory or (not as important to me) in any directory?
0
Comment
Question by:jc31415
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 6938978
File f = new File("myFile.txt");
boolean fileExists = f.exists();

If you want to check if it exists anywhere on the HD, you'll have to recurse all directories on each drive
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 6938979
To check whether a file exists in the current directory, here is an example:

import java.io.*;

public class FileTest {
   public static void main(String[] args) {
       try {
          File f = new File("myFile.txt");

          if (f.exists())
             System.out.println("Exists");
          else
             System.out.println("Not exists");
        } catch (Exception e) { }
   }
}
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question