Java Code Examples for java.nio.file.FileAlreadyExistsException

Following code examples demonstrate how to use java.nio.file.FileAlreadyExistsExceptionfrom java. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use java.nio.file.FileAlreadyExistsExceptionand various code implementation of this class.

	public static void main(String[] args) {
		Path source = Paths.get(args[0]);
		Path destination = Paths.get(args[1]);
		try {
			Files.move(source, destination,StandardCopyOption.REPLACE_EXISTING);
		} catch (FileAlreadyExistsException fae) {
			fae.printStackTrace();
		} catch (NoSuchFileException nsfe) {
			nsfe.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	} 


	public static void main(String[] args) throws FileAlreadyExistsException {

		String str="";
		if(str.isEmpty()){
			System.out.println("Hello");
		}
		
		int a=5;
		System.out.println("value of a="+((a>5)));
		
		System.out.println("hello "+"{"+a+"}");
		
	throw new FileAlreadyExistsException("dsfsd");
	} 

	public static void main(String[] args) {
		Hata hata = new Hata();
		try {
			hata.parseError("asd");
		} catch (FileAlreadyExistsException e) {
			e.printStackTrace();
		}
		
		try {
			hata.deneme2();
		} catch (Exception e) {
			e.printStackTrace();
		}
	} 

    @Test
    public default void verifyGenericType()
    throws Exception {
        final Class<SUT> sut = createNewSUT();
        assertTrue("This j8unit test interface is used with a generic type that is illegaly not assignable to FileAlreadyExistsException.class!",
                   FileAlreadyExistsException.class.isAssignableFrom(sut));
    } 

    public OutputFileWriterBase(Path outputPath) throws IOException {
        Preconditions.checkArgument(outputPath != null, "outputPath can not be null.");
        if (Files.exists(outputPath)) {
            throw new FileAlreadyExistsException(outputPath + " already exists");
        }
        Files.createFile(outputPath);
        this.outputFile = outputPath.toFile();
    } 

	public static void main(String[] args) {
		Path source = Paths.get(args[0]);
		Path destination = source.resolveSibling(Paths.get(args[1])); 
		
		try {
		} catch (FileAlreadyExistsException fae) {
			fae.printStackTrace();
		} catch (NoSuchFileException nsfe) {
			nsfe.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	} 

	public static void main(String[] args) throws IOException {
		Path source  = Paths.get("file.txt");
		Path target = Paths.get("file-copy.txt");

		try {
			Files.copy(source, target);
		} catch(FileAlreadyExistsException fae) {
			fae.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		Files.list(Paths.get(".")).forEach(System.out::println);
	} 

    public static void fetchRfc(int num) throws FileAlreadyExistsException {
        makeDirs();
        fetchRfc(num, false);
    } 

    public static void fetchIndex(boolean safe) throws FileAlreadyExistsException {
        makeDirs();
        String url = Constants.RFC_INDEX_URL;
        if (safe) {
            fetchIndexSafely(url);
        } else {
            fetchIndexUnsafely(url);
        }
    } 

    public static void fetchRfc(int num, boolean overwrite) throws FileAlreadyExistsException {
        makeDirs();
        String base_location = Constants.RFC_BASE_LOCATION;
        String s = "rfc" + num + ".txt";
        File filename = new File(Constants.RFC_CACHE + s);
        if (filename.exists()) {
            if (!overwrite) {
                throw new FileAlreadyExistsException(filename.getAbsolutePath());
            } else {
                filename.delete();
            }
        }

        try {
            URL u = new URL(base_location + s);
            FileUtils.copyURLToFile(u, filename);
        } catch (MalformedURLException ex) {
            Logger.getLogger(RfcFetcher.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(RfcFetcher.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    private static void fetchIndexSafely(String url) throws FileAlreadyExistsException {
        File f = new File(Constants.RFC_INDEX_LOCAL_NAME);
        if (f.exists()) {
            throw new FileAlreadyExistsException(f.getAbsolutePath());
        } else {
            try {
                URL u = new URL(url);
                FileUtils.copyURLToFile(u, f);
            } catch (MalformedURLException ex) {
                Logger.getLogger(RfcFetcher.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(RfcFetcher.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    } 

    private static void fetchIndexUnsafely(String url) {
        try {
            File f = new File(Constants.RFC_INDEX_LOCAL_NAME);
            if (f.exists()) {
                f.delete();
            }
            fetchIndexSafely(url);
        } catch (FileAlreadyExistsException ex) {
            Logger.getLogger(RfcFetcher.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    @Override
    public void method() throws AWTException, SQLException, FileNotFoundException,
            EOFException, FileAlreadyExistsException {
        super.method();
    } 

    public static <T> Either<IOException, T> get(IOSupplier<T> supplier) {
        try {
            return Either.right(supplier.get());
        } catch (FileAlreadyExistsException ex) {
            IOException ex1 = new FileAlreadyExistsException(ex.getFile(), ex.getOtherFile(), "File already exists.");
            return Either.left(ex1);
        } catch (IOException ex) {
            return Either.left(ex);
        }
    }  

Advertisement
Javadoc
Checked exception thrown when an attempt is made to create a file or directory and a file of that na

me already exists. @since 1.7

Read More
Advertisement