Solved

File exists?

Posted on 2002-04-13
2
252 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 50 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

738 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