package パッケージ名;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
/**
* 引数で指定したファイルを指定した文字列で置換える。
* java tool.Replace [ファイル名] [置換元文字列] [置換文字列]
* 例 C:\temp.txt testtarget testreplace する します
*/
public class Replace {
/** 対象文字列 */
private static String target;
/** 置換文字列 */
private static String replace;
/** 指定ファイル名 */
private static String filename;
public static void main(String[] args) throws IOException {
filename = args[0];
target = args[1];
replace = args[2];
searchDir(filename);
}
/**
* 指定ファイルの中に含まれるtargetをreplaceに置換する。
* @throws IOException
*/
private static void searchDir(String filename) throws IOException {
String[] lines = getLines(filename);
for (int j = 0; j < lines.length; j++) {
lines[j] = lines[j].replaceAll(target,replace);
}
PrintWriter output =
new PrintWriter(new BufferedWriter(new FileWriter(filename)));
for (int j = 0; j < lines.length; j++) {
output.println(lines[j]);
}
output.close();
}
/**
* 指定したファイルをテキストストリームで全て読み込んでString配列に
* 格納して返します。
* @param filePath 指定したテキストファイルのパス
* @return 読み込んだファイルの中身
*/
private static String[] getLines(String filePath) throws IOException {
BufferedReader input = new BufferedReader(new FileReader(filePath));
ArrayList list = new ArrayList(5000);
String line = null;
while ((line = input.readLine()) != null) {
list.add(line);
}
String[] lines = new String[list.size()];
list.toArray(lines);
input.close();
return lines;
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
/**
* 引数で指定したファイルを指定した文字列で置換える。
* java tool.Replace [ファイル名] [置換元文字列] [置換文字列]
* 例 C:\temp.txt testtarget testreplace する します
*/
public class Replace {
/** 対象文字列 */
private static String target;
/** 置換文字列 */
private static String replace;
/** 指定ファイル名 */
private static String filename;
public static void main(String[] args) throws IOException {
filename = args[0];
target = args[1];
replace = args[2];
searchDir(filename);
}
/**
* 指定ファイルの中に含まれるtargetをreplaceに置換する。
* @throws IOException
*/
private static void searchDir(String filename) throws IOException {
String[] lines = getLines(filename);
for (int j = 0; j < lines.length; j++) {
lines[j] = lines[j].replaceAll(target,replace);
}
PrintWriter output =
new PrintWriter(new BufferedWriter(new FileWriter(filename)));
for (int j = 0; j < lines.length; j++) {
output.println(lines[j]);
}
output.close();
}
/**
* 指定したファイルをテキストストリームで全て読み込んでString配列に
* 格納して返します。
* @param filePath 指定したテキストファイルのパス
* @return 読み込んだファイルの中身
*/
private static String[] getLines(String filePath) throws IOException {
BufferedReader input = new BufferedReader(new FileReader(filePath));
ArrayList list = new ArrayList(5000);
String line = null;
while ((line = input.readLine()) != null) {
list.add(line);
}
String[] lines = new String[list.size()];
list.toArray(lines);
input.close();
return lines;
}
}
No comments yet