好了,经过50分钟的调试,基本上没什么bug了,我的速度很慢啊。希望能让你满意。
package cn.web;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
* @target:为了取出java文件中的字符串常量值和字符常量值,也就是""和''之间的内容
* @author Saler
* @date 2010-12-27
*/
public class QueryString {
static ArrayList<StringAndLine> al = new ArrayList<StringAndLine>();
public static void main(String[] args) throws IOException {
File f = new File("test.java");
if(!f.exists()){
f.createNewFile();
}
int i = 1;
String str = "";
BufferedReader br = new BufferedReader(new FileReader(f));
while((str=br.readLine())!=null){
FindoutString(str,i++);
}
for(int j = 0;j<al.size();j++){
System.out.println("字符串:\""+al.get(j).str+"\"\t行号:"+al.get(j).line);
}
}
private static void FindoutString(String str, int line) {
boolean bool = false;
int start = 0;
int end = 0;
String subStr;
for(int i=0;i<str.length();i++){
int index = i;
if(str.charAt(index)=='"'||str.charAt(index)=='\''){//||str.charAt(i)=='\''
start = index+1;
while((str.charAt(++index)!='"'||str.charAt(++index)=='\'')&&index<str.length()-1);
end = index-1;
}
i = index;
if(end-start!=0){
subStr = str.substring(start,end);
StringAndLine sal = new StringAndLine(subStr, line);
al.add(sal);
start = 0;
end = 0;
}
}
}
}
class StringAndLine{
String str;
int line;
public StringAndLine(String str,int line){
this.str = str;
this.line = line;
}
}
我把测试文件和运行结果也给你发过去。
测试文件:test.java
package com.first;
class erro //错误处理类
{
String[] err={" 缺少左括号"};
char sex = '男';
erro(int linenumber,int t)
{
System.out.println("行:"+linenumber+" 错误号"+t+" "+err[t]);
}
}
运行结果:
字符串:" 缺少左括号" 行号:5
字符串:"男" 行号:6
字符串:"行:" 行号:9
字符串:" 错误号" 行号:9
字符串:" " 行号:9
希望能让你满意,呵呵,祝你好运!
温馨提示:答案为网友推荐,仅供参考