Java File Exist fileExists(String name)

Here you can find the source of fileExists(String name)

Description

return true if the specified file (or Directory) exists either on a network or a local server.

License

Apache License

Parameter

Parameter Description
name the name of the file.

Declaration

public static boolean fileExists(String name) 

Method Source Code

//package com.java2s;
/*/*from  ww  w  .jav  a  2 s .  co m*/
 * Copyright 2004 - 2009 University of Cardiff.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.*;

public class Main {
    /**
     * return true if the specified file (or Directory) exists either on a network or a local server.  The function
     * automatically detects where the file is.
     *
     * @param name the name of the file.
     */
    public static boolean fileExists(String name) {
        try {
            if (name.indexOf("://") != -1) { // a network path
                return true;

                /* URL ur = new URL(correctURL(name));
                URLConnection url = ur.openConnection();
                    
                String typ = url.getContentType();
                    
                StringVector sv = FileUtils.readAndSplitFile(
                FileUtils.createReader(ur));
                    
                System.out.println(sv.at(0));
                System.out.println(typ);
                    
                if (typ.equals("text/plain"))
                return true;
                    
                if (typ.equals("text/html"))
                if (GUIEnv.isAnApplet())
                return true;
                else if (isDirectory(sv.at(0)))
                return true;
                return false;*/
            } else {
                File f = new File(name);
                return f.exists();
            }
        } catch (Exception io) {
            System.out.println("Exception for " + name);
            return false;
        }
    }
}

Related

  1. fileExists(String filepath)
  2. fileExists(String filePath)
  3. fileExists(String filePathString)
  4. fileExists(String fName)
  5. fileExists(String folderPath, String fileName)
  6. fileExists(String parentDirectory, String filename)
  7. fileExists(String path)
  8. fileExists(String path)
  9. fileExists(String path)