读书人

Java中的资料操作

发布时间: 2012-10-06 17:34:01 作者: rapoo

Java中的文件操作

使用Java实现一些文件操作。

?

import java.io.*;public class FileOperator {public FileOperator() {}/** * Create new folder * @param folderPath String as c:/test */public void newFolder(String folderPath) {try {File myFilePath = new File(folderPath);if (!myFilePath.exists()) {myFilePath.mkdir();}} catch (Exception e) {System.out.println("error in creating folder");e.printStackTrace();}}/** * Create new file * @param filePath String as c:/test.txt * @param fileContent */public void newFile(String filePath, String fileContent) {try {File file = new File(filePath);if (!file.exists()) {file.createNewFile();}FileWriter writer = new FileWriter(filePath);PrintWriter out = new PrintWriter(writer);out.println(fileContent);writer.close();} catch (Exception e) {System.out.println("error in creating new file");e.printStackTrace();}}/** * Delete a file * @param filePath */public void delFile(String filePath) {try {File file = new File(filePath);file.delete();} catch (Exception e) {System.out.println("error in deleting file");e.printStackTrace();}}/** * Delete folder * @param folderPath */public void delFolder(String folderPath) {try {delAllFile(folderPath);//删除完里面所有内容File folder = new File(folderPath);folder.delete();//删除空文件夹} catch (Exception e) {System.out.println("error in deleting folder");e.printStackTrace();}}/** * Delete all files * @param folderPath */public void delAllFile(String folderPath) {try {File folder = new File(folderPath);if (!folder.exists()) {return;}if (!folder.isDirectory()) {return;}String[] fileList = folder.list();String path = null;if (!folderPath.endsWith(File.separator)) {path = folderPath + File.separator;}for (int i = 0; i < fileList.length; i++) {File file = new File(path + fileList[i]);if (file.isFile()) {file.delete();} else if (file.isDirectory()) {delAllFile(path + fileList[i]);//先删除文件夹里面的文件delFolder(path + fileList[i]);//再删除空文件夹}}} catch (Exception e) {System.out.println("error in deleting all files");e.printStackTrace();}}/** * Copy a file * @param sourcePath * @param destPath */public void copyFile(String sourcePath, String destPath) {try {int bytesum = 0;int byteread = 0;File file = new File(sourcePath);if (file.exists()) { //文件存在时InputStream in = new FileInputStream(sourcePath);FileOutputStream out = new FileOutputStream(destPath);byte[] buffer = new byte[1024];while ((byteread = in.read(buffer)) != -1) {bytesum += byteread;//字节数 文件大小System.out.println(bytesum);out.write(buffer, 0, byteread);}out.flush();out.close();in.close();}} catch (Exception e) {System.out.println("error in copy file");e.printStackTrace();}}/** * Copy folder * @param sourcePath * @param destPath */public void copyFolder(String sourcePath, String destPath) {try {new File(destPath).mkdir();File folder = new File(sourcePath);String[] fileList = folder.list();String source = null;String dest = null;if (!sourcePath.endsWith(File.separator))source = sourcePath + File.separator;if (!destPath.endsWith(File.separator))dest = destPath + File.separator;for (int i = 0; i < fileList.length; i++) {File file = new File(source + fileList[i]);if (file.isFile()) {copyFile(source+fileList[i], dest+fileList[i]);} else if (file.isDirectory()) {copyFolder(source+fileList[i], dest+fileList[i]);}}} catch (Exception e) {System.out.println("error in copying folder");e.printStackTrace();}}/** * Move file * @param sourcePath * @param destPath */public void moveFile(String sourcePath, String destPath) {copyFile(sourcePath, destPath);delFile(sourcePath);}/** * Move folder * @param sourcePath * @param destPath */public void moveFolder(String sourcePath, String destPath) {copyFolder(sourcePath, destPath);delFolder(sourcePath);}/** * displayFiles * @param path */public void displayFiles(String path) {File file = new File(path);if (file.isFile()) {System.out.println(file.getName());} else {System.out.println("<DIR> " + file.getName());String[] fileList = file.list();String pathName = file.getAbsolutePath();for (int i = 0; i < fileList.length; i++) {displayFiles(pathName + File.separator + fileList[i]);}}}/** * Read a file by char * @param filePath */public void readFile(String filePath) {try {InputStream in = new FileInputStream(filePath);byte b; while ((b = (byte)in.read()) != -1) {System.out.print((char)b);}} catch (Exception e) {System.out.println("error in read file");e.printStackTrace();}}}
?

读书人网 >编程

热点推荐