Wednesday, June 30, 2010

Java File Examples


1. How to check if a file exists
2. How to delete a file or folder
3. How to rename a file or folder
4. How to copy a file
5. How to list files with same extensions/how to list only same type files / How to use FilenameFilter
6. How to move a file in Java

1. How to check if a file exists

package io;

import java.io.File;

public class FileExample {

/**
* This is to check whether a file( not folder) exists.
*/
public static boolean isFileExist(String filePath) throws Exception {

boolean exists = false;
File file = null;
try {
file = new File(filePath);
exists = file.isFile();
} catch (Exception e) {
throw new Exception("Exception thrown while checking the file!", e);
} finally {
if (file != null) {
file = null;
}
}
return exists;
}
}


2. How to delete a file or folder

package io;

import java.io.File;

public class FileExample {

/**
* This is to delete a file or folder.
*/
public static void deleteFile(String filePath) throws Exception {
File file = null;
try {
file = new File(filePath);
if (file != null) {
file.delete();
}
} catch (Exception e) {
throw new Exception("Exception thrown while deleting the file!", e);
} finally {
if (file != null) {
file = null;
}
}
}

}

3. How to rename a file or folder

package io;

import java.io.File;

public class FileExample {

/**
* This is to rename a file or folder.
*/
public static void rename(String loaction, String currentName, String newName) throws Exception {
File currentFile = null;
File newFile = null;
try {
currentFile = new File(loaction + File.separator + currentName);
newFile = new File(loaction + File.separator + newName);
if (currentFile != null) {
currentFile.renameTo(newFile);
}
} catch (Exception e) {
throw new Exception("Exception thrown while deleting the file!", e);
} finally {
if (currentFile != null || newFile != null) {
currentFile = null;
newFile = null;
}
}
}
}


4. How to copy a file

class CopyFile {

public static void main(String[] args) {
try {
CopyFile copyFile = new CopyFile();
copyFile.copyFile("test.txt", "C:/temp/", "C:/temp/subtmp/");
System.out.println("done");
} catch (Exception e) {
e.printStackTrace();
}
}

public void copyFile(String fileName, String fromLocation, String toLocation) throws Exception {

FileChannel srcChannel = null;
FileChannel dstChannel = null;
try {
srcChannel = new FileInputStream(fromLocation + File.separator + fileName).getChannel();
dstChannel = new FileOutputStream(toLocation + File.separator + fileName).getChannel();

// Copy file contents from source to destination
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

} catch (Exception e) {
throw new Exception(e.getMessage(), e);
} finally {
// Closing the channels
try {
if (srcChannel != null) {
srcChannel.close();
}
if (dstChannel != null) {
dstChannel.close();
}
} catch (IOException ioException) {
throw new Exception("Exception thrown while closing the channels inside finally!", ioException);
}
}
}
}

5. How to list files with same extensions/how to list only same type files / How to use FilenameFilter

package io;

import java.io.*;
import java.util.*;

public class FileExample {

public static void main(String[] args) {
try {
FileExample example = new FileExample();
example.listPDFFiles("C:/temp/subtmp/");
System.out.println("done");
} catch (Exception e) {
e.printStackTrace();
}
}

public void listPDFFiles(String folderPath) throws Exception {
File file = null;
try {
file = new File(folderPath);
FilenameFilter pdfFilter = new PDFFilter();
String[] list = file.list(pdfFilter);
for (int i = 0; i < list.length; i++) {
System.out.println(list[i]);
}
} catch (Exception e) {
throw new Exception("Exception thrown while checking the file!", e);
} finally {
if (file != null) {
file = null;
}
}
}

class PDFFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.toLowerCase().endsWith(".pdf"));
}
}
}

6. How to move a file in Java

package io;

import java.io.*;
import java.util.*;

public class FileExample {

public static void main(String[] args) {
try {
FileExample.move("C:/temp/subtmp/newtest.txt", "C:/temp/subtmp/a/newtest.txt");
System.out.println("done");
} catch (Exception e) {
e.printStackTrace();
}
}

public static void move(String srcFilePath, String destFilePath) throws Exception {
File scrFile = null;
File destFile = null;
try {
scrFile = new File(srcFilePath);
destFile = new File(destFilePath);
if (scrFile != null) {
scrFile.renameTo(destFile);
}
} catch (Exception e) {
throw new Exception("Exception thrown while moving the file!", e);
} finally {
if (scrFile != null || destFile != null) {
scrFile = null;
destFile = null;
}
}
}
}

No comments:

Post a Comment