java對(duì)指定目錄下文件讀寫操作介紹
更新時(shí)間:2012年11月25日 18:29:56 作者:
本文將詳細(xì)介紹java對(duì)指定目錄下文件的讀寫功能實(shí)現(xiàn),需要的朋友可以參考下
最近因?yàn)轫?xiàng)目的國(guó)際化的需要,需要對(duì)整個(gè)項(xiàng)目的100來個(gè)插件做國(guó)際化,這是一件痛苦的事情,因?yàn)榧凅w力勞動(dòng)。為了省點(diǎn)工作量,想著能不能寫個(gè)程序批處理了,減少點(diǎn)工作量,于是就有了下面的代碼。
1.讀取指定的(.java)文件:
public static String readFile(String path) throws IOException {
File f = new File(path);
StringBuffer res = new StringBuffer();
String filePathStr = f.getPath();
System.out.println("獲取文件的路徑:::::::"+filePathStr);
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis,Charset.forName("GBK")); //以gbk編碼打開文本文件
BufferedReader br = new BufferedReader(isr, 8192 * 8);
String line = null;
int linenum = 0;
while((line=br.readLine())!=null) {
linenum ++;
res.append(line+"此處可以添加你自己的字符串處理邏輯"+"\r\n");
}
br.close();
return res.toString();
}
2.讀取的文件內(nèi)容信息寫到指定的(.java)文件
public static boolean writeFile(String cont, String path) {
try {
File dist = new File(path);
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(dist),"GBK");
writer.write(cont);
writer.flush();
writer.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
3.查找指定目錄下所有符合條件的.java文件,并更新文件信息
/**
* 查找文件
* @param f
* @throws IOException
*/
public static void findFile(File f) throws IOException {
if(f.exists()) {
if(f.isDirectory()) {
for(File fs:f.listFiles(ff)) {
findFile(fs);
}
} else {
updateFile(f);
}
}
}
/**
* 逐行讀java文件
* @param f
* @throws IOException
*/
private static void updateFile(File f) throws IOException {
String filePathStr = f.getPath();
System.out.println("開始讀取文件的路徑:::::::"+filePathStr);
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis,Charset.forName("GBK")); //以gbk編碼打開文本文件
BufferedReader br = new BufferedReader(isr, 8192 * 8);
String line = null;
int linenum = 0;
StringBuffer res = new StringBuffer();
while((line=br.readLine())!=null) {
String updateStr= updateStr(line);
res.append(updateStr+"\r\n");
if(!line.trim().equals(updateStr.trim())) {
linenum ++;
}
}
br.close();
//如果文件有修改,則修改后的文件,覆蓋原有文件
if(linenum>0) {
System.out.println("=============================");
System.out.println("filePathStr:"+filePathStr);
System.out.println("文件修改了:"+linenum+"處。");
System.out.println("=============================");
String cont = res.toString();
ReadWriteFile.write(cont, filePathStr);
}
}
/**
* 驗(yàn)證讀取的字符串信息
* 和更新字符串信息
* @param str
*/
private static String updateStr(String str) {
//判斷字符串是否是需要更新的字符串
boolean isok = filterStr(str);
int strNum = StringValidation.strNum(str, StringValidation.ch);
if(isok || strNum == 0) {
return str;
} else {
String temp = "";
for(int i=1;i<=strNum/2;i++) {
temp += " //$NON-NLS-"+i+"$"; //需要添加的字符
}
str = str+temp;
}
return str;
}
//過濾文件類型
private static FileFilter ff = new FileFilter() {
public boolean accept(File pathname) {
String path = pathname.getName().toLowerCase();
logger.info("FileFilter path::::"+path);
//只匹配 .java 結(jié)尾的文件
if (pathname.isDirectory() || path.endsWith(".java")) {
return true;
}
return false;
}
};
/**
* 過濾掉不需要處理的字符串
* @param str
* @return
*/
public static boolean filterStr(String str) {
boolean isok = false;
//過濾字符串
isok = (str.indexOf("import ")>=0)
|| (str.indexOf("package ")>=0)
|| (str.indexOf(" class ")>=0)
|| (str.indexOf("http://$NON-NLS")>=0)
|| (str.indexOf("http://")==0)
|| (str.indexOf("/*")>=0)
|| (str.indexOf("*")>=0)
|| (str.trim().indexOf("@")==0)
|| (str.indexOf("\"")==-1)
|| ("".equals(str))
|| isCh(str);
return isok;
}
/**
* 驗(yàn)證字符串是否含有中文字符
* @param str
* @return
*/
public static boolean isCh(String str) {
Pattern pa = Pattern.compile("[\u4E00-\u9FA0]");
Matcher m = pa.matcher(str);
boolean isok = m.find();
return isok;
}
總結(jié):當(dāng)我們拿到一個(gè)別人給的需求,先不要急于去處理,先分析,再分析,然后做出最優(yōu)的解決方案,處理好這項(xiàng)工作。
1.讀取指定的(.java)文件:
復(fù)制代碼 代碼如下:
public static String readFile(String path) throws IOException {
File f = new File(path);
StringBuffer res = new StringBuffer();
String filePathStr = f.getPath();
System.out.println("獲取文件的路徑:::::::"+filePathStr);
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis,Charset.forName("GBK")); //以gbk編碼打開文本文件
BufferedReader br = new BufferedReader(isr, 8192 * 8);
String line = null;
int linenum = 0;
while((line=br.readLine())!=null) {
linenum ++;
res.append(line+"此處可以添加你自己的字符串處理邏輯"+"\r\n");
}
br.close();
return res.toString();
}
2.讀取的文件內(nèi)容信息寫到指定的(.java)文件
復(fù)制代碼 代碼如下:
public static boolean writeFile(String cont, String path) {
try {
File dist = new File(path);
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(dist),"GBK");
writer.write(cont);
writer.flush();
writer.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
3.查找指定目錄下所有符合條件的.java文件,并更新文件信息
復(fù)制代碼 代碼如下:
/**
* 查找文件
* @param f
* @throws IOException
*/
public static void findFile(File f) throws IOException {
if(f.exists()) {
if(f.isDirectory()) {
for(File fs:f.listFiles(ff)) {
findFile(fs);
}
} else {
updateFile(f);
}
}
}
/**
* 逐行讀java文件
* @param f
* @throws IOException
*/
private static void updateFile(File f) throws IOException {
String filePathStr = f.getPath();
System.out.println("開始讀取文件的路徑:::::::"+filePathStr);
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis,Charset.forName("GBK")); //以gbk編碼打開文本文件
BufferedReader br = new BufferedReader(isr, 8192 * 8);
String line = null;
int linenum = 0;
StringBuffer res = new StringBuffer();
while((line=br.readLine())!=null) {
String updateStr= updateStr(line);
res.append(updateStr+"\r\n");
if(!line.trim().equals(updateStr.trim())) {
linenum ++;
}
}
br.close();
//如果文件有修改,則修改后的文件,覆蓋原有文件
if(linenum>0) {
System.out.println("=============================");
System.out.println("filePathStr:"+filePathStr);
System.out.println("文件修改了:"+linenum+"處。");
System.out.println("=============================");
String cont = res.toString();
ReadWriteFile.write(cont, filePathStr);
}
}
/**
* 驗(yàn)證讀取的字符串信息
* 和更新字符串信息
* @param str
*/
private static String updateStr(String str) {
//判斷字符串是否是需要更新的字符串
boolean isok = filterStr(str);
int strNum = StringValidation.strNum(str, StringValidation.ch);
if(isok || strNum == 0) {
return str;
} else {
String temp = "";
for(int i=1;i<=strNum/2;i++) {
temp += " //$NON-NLS-"+i+"$"; //需要添加的字符
}
str = str+temp;
}
return str;
}
//過濾文件類型
private static FileFilter ff = new FileFilter() {
public boolean accept(File pathname) {
String path = pathname.getName().toLowerCase();
logger.info("FileFilter path::::"+path);
//只匹配 .java 結(jié)尾的文件
if (pathname.isDirectory() || path.endsWith(".java")) {
return true;
}
return false;
}
};
/**
* 過濾掉不需要處理的字符串
* @param str
* @return
*/
public static boolean filterStr(String str) {
boolean isok = false;
//過濾字符串
isok = (str.indexOf("import ")>=0)
|| (str.indexOf("package ")>=0)
|| (str.indexOf(" class ")>=0)
|| (str.indexOf("http://$NON-NLS")>=0)
|| (str.indexOf("http://")==0)
|| (str.indexOf("/*")>=0)
|| (str.indexOf("*")>=0)
|| (str.trim().indexOf("@")==0)
|| (str.indexOf("\"")==-1)
|| ("".equals(str))
|| isCh(str);
return isok;
}
/**
* 驗(yàn)證字符串是否含有中文字符
* @param str
* @return
*/
public static boolean isCh(String str) {
Pattern pa = Pattern.compile("[\u4E00-\u9FA0]");
Matcher m = pa.matcher(str);
boolean isok = m.find();
return isok;
}
總結(jié):當(dāng)我們拿到一個(gè)別人給的需求,先不要急于去處理,先分析,再分析,然后做出最優(yōu)的解決方案,處理好這項(xiàng)工作。
相關(guān)文章
Mybatis內(nèi)置參數(shù)之_parameter和_databaseId的使用
這篇文章主要介紹了Mybatis內(nèi)置參數(shù)之_parameter和_databaseId的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12深入理解Java虛擬機(jī) JVM 內(nèi)存結(jié)構(gòu)
本節(jié)將會(huì)介紹一下JVM的內(nèi)存結(jié)構(gòu),JVM運(yùn)行時(shí)數(shù)據(jù)區(qū)的各個(gè)組成部分:堆,方法區(qū),程序計(jì)數(shù)器,Java虛擬機(jī)棧,本地方法棧,還會(huì)對(duì)Java堆的分代劃分做個(gè)簡(jiǎn)單的介紹2021-09-09Java中List Set和Map之間的區(qū)別_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Java集合的主要分為三種類型set集,list列表,map映射,接下來通過本文給大家詳細(xì)介紹java中l(wèi)ist、Set和Map之間的區(qū)別,需要的的朋友參考下吧2017-05-05使用SpringBoot 配置Oracle和H2雙數(shù)據(jù)源及問題
這篇文章主要介紹了使用SpringBoot 配置Oracle和H2雙數(shù)據(jù)源及問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11SpringBoot Web開發(fā)之系統(tǒng)任務(wù)啟動(dòng)與路徑映射和框架整合
這篇文章主要介紹了SpringBoot Web開發(fā)中的系統(tǒng)任務(wù)啟動(dòng)與路徑映射和Servlet、Filter、Listener框架整合,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08常用數(shù)據(jù)庫的驅(qū)動(dòng)程序及JDBC URL分享
這篇文章主要介紹了常用數(shù)據(jù)庫的驅(qū)動(dòng)程序及 JDBC URL,需要的朋友可以看下2014-01-01Java Arrays.AsList原理及用法實(shí)例
這篇文章主要介紹了Java Arrays.AsList原理及用法實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11