java對指定目錄下檔案讀寫操作介紹_java

來源:互聯網
上載者:User
最近因為項目的國際化的需要,需要對整個項目的100來個外掛程式做國際化,這是一件痛苦的事情,因為純體力勞動。為了省點工作量,想著能不能寫個程式批處理了,減少點工作量,於是就有了下面的代碼。
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.讀取的檔案內容資訊寫到指定的(.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);
}
}
/**
* 驗證讀取的字串資訊
* 和更新字串資訊
* @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 結尾的檔案
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("//$NON-NLS")>=0)
|| (str.indexOf("//")==0)
|| (str.indexOf("/*")>=0)
|| (str.indexOf("*")>=0)
|| (str.trim().indexOf("@")==0)
|| (str.indexOf("\"")==-1)
|| ("".equals(str))
|| isCh(str);
return isok;
}
/**
* 驗證字串是否含有中文字元
* @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;
}

總結:當我們拿到一個別人給的需求,先不要急於去處理,先分析,再分析,然後做出最優的解決方案,處理好這項工作。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.