Created
June 13, 2013 07:07
-
-
Save tomoyamkung/5771751 to your computer and use it in GitHub Desktop.
[Java]ファイルに関するユーティリティ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* File オブジェクトとそのオブジェクトが表す実ファイルを作成する。 | |
* | |
* @param parent ファイルを作成するディレクトリ | |
* @param fileName ファイル名 | |
* @return 作成したファイルオブジェクト | |
* @throws IOException ファイルの作成に失敗した場合 | |
*/ | |
public static File create(File parent, String fileName) throws IOException { | |
File file = new File(parent, fileName); | |
file.createNewFile(); | |
return file; | |
} | |
/** | |
* ファイルを作成する(実ファイルを作成する)。 | |
* | |
* @param file 作成する File オブジェクト | |
* @param content ファイルに書き込む内容 | |
* @throws IOException ファイルの作成に失敗した場合 | |
*/ | |
public static void write(File file, String content) throws IOException { | |
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file))); | |
pw.println(content); | |
pw.close(); | |
} | |
/** | |
* ファイルを読み込む。 | |
* | |
* @param file 読み込むファイル | |
* @return 読み込んだファイルの内容 | |
* @throws IOException ファイルの読み込みに失敗した場合 | |
*/ | |
public static String read(File file) throws IOException { | |
StringBuilder content = new StringBuilder(); | |
String line = ""; | |
BufferedReader in = null; | |
try { | |
in = new BufferedReader(new FileReader(file)); | |
while ((line = in.readLine()) != null) { | |
content.append(line); | |
} | |
} finally { | |
if (in != null) { | |
in.close(); | |
} | |
} | |
return content.toString(); | |
} | |
/** | |
* ファイルをリネームする。 | |
* | |
* @param src リネーム元の File オブジェクト | |
* @param dest リネーム先の File オブジェクト | |
* @throws IOException ファイルのリネームに失敗した場合 | |
*/ | |
public static void rename(File src, File dest) throws IOException { | |
FileUtils.copyFile(src, dest); | |
FileUtils.forceDelete(src); | |
} | |
/** | |
* ファイルを削除する。 | |
* | |
* @param path 削除するファイルのパス | |
* @throws IOException ファイルの削除に失敗した場合 | |
*/ | |
public static void delete(String path) throws IOException { | |
File file = new File(path); | |
if(file.exists()) { | |
FileUtils.forceDelete(file); | |
} | |
} | |
/** | |
* ファイルを移動する。 | |
* | |
* @param src 移動元のパス | |
* @param dest 移動先のパス | |
* @throws IOException ファイル移動に失敗した場合 | |
*/ | |
public static void move(String src, String dest) throws IOException { | |
Path srcPath = FileSystems.getDefault().getPath(src); | |
Path destPath = FileSystems.getDefault().getPath(dest); | |
Files.move(srcPath, destPath, StandardCopyOption.REPLACE_EXISTING); | |
} | |
/** | |
* ファイルをコピーする。 | |
* | |
* @param srcPath コピー元のファイルパス | |
* @param destPath コピー先のファイルパス | |
* @throws IOException ファイルのコピーに失敗した場合 | |
*/ | |
public static void copy(String srcPath, String destPath) throws IOException { | |
FileUtils.copyFile(new File(srcPath), new File(destPath)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment