-1
    public class GetProp extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String file_proc = readFile();
        TextView tv = (TextView)findViewById(R.id.tv);
        tv.setText("Read File contents from SDCARD : \n" + file_proc);
    }
    public String readFile(){
        BufferedReader rdr;
        String proc = "";
        String line;
        int lineNumber = 0;
        try {
            rdr = new BufferedReader(new FileReader("/proc/cpuinfo"));     
            while ((line = rdr.readLine()) != null) {
                lineNumber++;
            Matcher matcher = Pattern.compile("Processor: (.*)").matcher(line);
            if (matcher.find()) {
                proc = matcher.group(1);
            }
            }           
        } catch (Exception e) {
            e.printStackTrace();
        }
        return proc;        
    }
}

我想从 /proc/cpuinfo txt 文件中打印一行,例如处理器:“RESULT”,其中 RESULT 是 matcher.group(1)。但是我的结果中没有文字,我的问题在哪里?

4

1 回答 1

2

这就是我的/proc/cpuinfo样子:

processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 42
/* etc */

您的正则表达式有两个问题:

  1. 正则表达式区分大小写。要么将其更改为processor,要么使用Pattern.compile(..., Pattern.CASE_INSENSITIVE)
  2. processor和冒号之间有空格。您应该将您的正则表达式更改为 processor\\s*: (.*)
于 2013-07-21T19:10:43.763 回答