ファイルの引用符付き(")の文字列を読みだします。
最近、会社がファイル中の文字列を暗号化します。一つ一つの文字列を検索すれば、効率が分かります。だから、スレ主は自分でjavaファイルの文字列を素早く調べました。
Eclipseコンソール出力の結果:
Eclipseコンソール出力の結果:
, :
public class FunctionInfo {
public static Sting funcs = "shae;toAddFans;method2;method3;method4";
public static Sting GFN(int flag) {
if (funcs.length() < 3) {
etun null;
}
//funcs = "classPath|methodName;classPath|methodName;classPath|methodName;";
Sting[] funcs_s = funcs.split(";");
//classPath|methodName
/*Log.d("fj", funcs_s[flag]);
// ------------------------------------------
Sting[] info = funcs_s[flag].split("-");*/
etun funcs_s[flag];
}
}
-----package /public class FunctionInfo {public static String funcs = "share;toAddFans;method2;method3;method4";public static String GFN(int flag) {if (funcs.length() < 3) {return null;}//funcs = "classPath|methodName;classPath|methodName;classPath|methodName;";String[] funcs_s = funcs.split(";");//classPath|methodName/*Log.d("fj", funcs_s[flag]);// ------------------------------------------String[] info = funcs_s[flag].split("-");*/return funcs_s[flag];}}
----- "share;toAddFans;method2;method3;method4"
----- "classPath|methodName;classPath|methodName;classPath|methodName;"
----- ";"
----- "fj"
----- "-"
操作が必要なファイルの内容public class FunctionInfo {
public static String funcs = "share;toAddFans;method2;method3;method4";
public static String GFN(int flag) {
if (funcs.length() < 3) {
return null;
}
//funcs = "classPath|methodName;classPath|methodName;classPath|methodName;";
String[] funcs_s = funcs.split(";");
//classPath|methodName
/*Log.d("fj", funcs_s[flag]);
// ------------------------------------------
String[] info = funcs_s[flag].split("-");*/
return funcs_s[flag];
}
}
工程コード:matcher Str法はマッチング抽出文字列である。/**
* , : "java/lang/String" Android log、println ......
*
* @param strContent
*/
public static void matcherStr(String strContent) {
//
if (strContent==null) {
System.out.println(" ");
return;
}
//
Pattern p = Pattern.compile("\"(.*?)\"");
// Pattern p = Pattern.compile("\"(.*?)(?
Matcher m = p.matcher(strContent);
ArrayList list = new ArrayList();
while (m.find()) {
list.add(m.group());
}
if (list.size()<=0) {
System.out.println(" : !!!");
}
else {
for (String s : list) {
System.out.println(" ----- " + s);
}
}
}
readFileToStringメソッドは、読み込んだファイルの内容を文字列に変換する方法です。/**
* file
*
* @param fileName
*
* @param charCode
*
* @return
*/
public static String readFileToString(String fileName, String charCode) {
try {
FileInputStream fis = new FileInputStream(new File(fileName));
InputStreamReader isr = new InputStreamReader(fis, charCode);
// file
BufferedReader bf = new BufferedReader(isr);
String content = "";
StringBuilder sb = new StringBuilder();
while (content != null) {
content = bf.readLine();
if (content == null) {
break;
}
sb.append(content.trim());
}
bf.close();
String fileStr = sb.toString();
if (fileStr.length()==0) {
System.out.println(" ...");
// , -null
return null;
}else {
System.out.println("
-----" + fileStr + "
");
// , ...
return fileStr;
}
} catch (Exception e1) {
return " ..." + e1.getMessage();
}
}
ファイルの内容を読み取る方法を説明します。/**
* , ,
*
*
*
* @param fileName
*
* @param stringCode
* -- ... utf-8 GBK
*/
public static void readFileSingleByChars(String fileName, String charCode) {
File file = new File(fileName);
Reader reader = null;
try {
System.out.println(" , :");
//
reader = new InputStreamReader(new FileInputStream(file), charCode); // ...
int tempchar;
while ((tempchar = reader.read()) != -1) {
// windows ,rn , 。 , 。 , r, n。 , 。
if (((char) tempchar) != 'r') {
System.out.print((char) tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* , ,
*
* @param fileName
*
* @param charCode
*
*/
public static void readFileMoreByChars(String fileName, String charCode) {
File file = new File(fileName);
Reader reader = null;
try {
System.out.println(" , :");
//
char[] tempchars = new char[1024];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName),
charCode); // ...
// ,charread
while ((charread = reader.read(tempchars)) != -1) {
if ((charread == tempchars.length)
&& (tempchars[tempchars.length - 1] != 'r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == 'r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
メソッド呼び出しテストクラスpublic static void main(String[] args) {
String fileName = " "
String charCode = "utf-8"; // utf-8 GBK
//
FileTest.readFileSingleByChars(fileName, charCode);
//
String text = FileTest.readFileToString(fileName, charCode);
matcherStr(text);
}