📄 configurationfile.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package FileTools;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.util.regex.Matcher;import java.util.regex.Pattern;import java.io.InputStream;import java.io.InputStreamReader;import java.net.URL;import java.net.URLConnection;/** * * @author Administrator */public class ConfigurationFile { public static boolean profileExist(String filePath){ File f1=new File(filePath); if(!f1.exists()) return false; if(f1.canRead()) return true; return false; } /** * 从ini配置文档中读取变量的值 * @param filePath 配置文档的路径 * @param section 要获取的变量所在段名称 * @param variable 要获取的变量名称 * @param defaultValue 变量名称不存在时的默认值 * @return 变量的值 * @throws IOException 抛出文档操作可能出现的io异常 */ public static String getProfileString( String filePath, String section, String variable, String defaultValue) { BufferedReader bufferedReader; try { bufferedReader = new BufferedReader(new FileReader(filePath)); } catch (Exception e) { return defaultValue; } return getProfileString(bufferedReader, section, variable, defaultValue); } public static String getProfileStringFromUrl( String Urlfile, String section, String variable, String defaultValue) { URL ServerUrl; BufferedReader bufReader; try { ServerUrl = new URL(Urlfile); URLConnection con = ServerUrl.openConnection(); con.setConnectTimeout(1000); con.setReadTimeout(3000); con.connect(); InputStream inStream = con.getInputStream(); bufReader = new BufferedReader(new InputStreamReader(inStream)); return getProfileString(bufReader, section, variable, defaultValue); } catch (Exception e) { return defaultValue; } } public static String getProfileString( BufferedReader bufferedReader, String section, String variable, String defaultValue) { String strLine, value = ""; boolean isInSection = false; try { while ((strLine = bufferedReader.readLine()) != null) { strLine = strLine.trim(); Pattern p; Matcher m; p = Pattern.compile("\\[.*\\]"); m = p.matcher((strLine)); if (m.matches()) { p = Pattern.compile("\\[" + section + "\\]"); m = p.matcher(strLine); if (m.matches()) { isInSection = true; } else { isInSection = false; } continue; } if (isInSection == true) { strLine = strLine.trim(); String[] strArray = strLine.split("="); if (strArray.length == 1) { value = strArray[0].trim(); if (value.equalsIgnoreCase(variable)) { value = ""; return value; } } else if (strArray.length == 2) { value = strArray[0].trim(); if (value.equalsIgnoreCase(variable)) { value = strArray[1].trim(); return value; } } else if (strArray.length > 2) { value = strArray[0].trim(); if (value.equalsIgnoreCase(variable)) { value = strLine.substring(strLine.indexOf("=") + 1).trim(); return value; } } } } } catch (Exception e) { try { bufferedReader.close(); } catch (Exception e1) { } } finally { } return defaultValue; } /** * 修改ini配置文档中变量的值 * @param filePath 配置文档的路径 * @param section 要修改的变量所在段名称 * @param variable 要修改的变量名称 * @param value 变量的新值 * @throws IOException 抛出文档操作可能出现的io异常 */ public static boolean setProfileString( String filePath, String section, String variable, String value) { String fileContent,allLine ,strLine , newLine; String getValue; boolean isInSection = false; fileContent = ""; File ftest=new File(filePath); if (!ftest.exists()) { try { if (!ftest.createNewFile()) { return false; } } catch (Exception e) { return false; } } if (!ftest.canWrite()) { return false; } if (!ftest.canRead()) { return false; } BufferedReader bufferedReader = null; try { bufferedReader = new BufferedReader(new FileReader(filePath)); while ((allLine = bufferedReader.readLine()) != null) { allLine = allLine.trim();// System.out.println("allLine == " + allLine); strLine = allLine; Pattern p; Matcher m; p = Pattern.compile("\\[.+\\]"); m = p.matcher((strLine)); if (m.matches()) { //if isInSection 增加1行,填补剩余内容 if (isInSection) { fileContent += allLine + "\r\n"; break; } p = Pattern.compile("\\[" + section + "\\]"); m = p.matcher(strLine); if (m.matches()) { isInSection = true; } else { isInSection = false; } fileContent += allLine + "\r\n"; continue; } if (isInSection == true) { strLine = strLine.trim(); String[] strArray = strLine.split("="); getValue = strArray[0].trim(); if (getValue.equalsIgnoreCase(variable)) { break; } } fileContent += allLine + "\r\n"; } if (!isInSection) { //新增Section newLine = "[" + section + "]"; fileContent += newLine + "\r\n"; } //下面一段是增加新行,填补剩余内容 newLine = variable + " = " + value + " "; fileContent += newLine + "\r\n"; while ((allLine = bufferedReader.readLine()) != null) { fileContent += allLine + "\r\n"; } bufferedReader.close(); } catch (IOException ex) { try { bufferedReader.close(); } catch (Exception e2) { return false; } return false; } BufferedWriter bufferedWriter=null; try { bufferedWriter = new BufferedWriter(new FileWriter(filePath, false)); bufferedWriter.write(fileContent); bufferedWriter.flush(); bufferedWriter.close(); return true; } catch (Exception e) { try { bufferedWriter.close(); } catch (Exception e1) { } return false; } //throw ex;// } finally {//// return true;// } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -