如何读取java文件中的特定行并打印出...? [英] how to read a specific line in .java file and prints out ...?
问题描述
我创建一个项目中,我需要阅读从java文件关键字启动特定行。
i am creating a project in which i need to read a specific line starting from a keyword in .java file .
我将要读的.java的文件:
i will have to read .java files for :
Intent myIntent = new Intent(StudentMap.this, StudentPref.class);
StudentMap.this.startActivity(myIntent);
我怎么会在code随时随地阅读这两条线。我应该要设置的正则表达式这一点,但怎么样?
how will i read these two lines anywhere in the code . should i have to set regex for this but how ?
我使用的:
public void onClick(View v) {
TextView responseText = (TextView) findViewById(R.id.responseText);
String myData = "";
String test = "Intent ";
// String name="";
switch (v.getId()) {
case R.id.getExternalStorage:
try {
FileInputStream fis = new FileInputStream(myExternalFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null){
myData = myData + strLine;
if (strLine.equals(test)) {
tv1.setText(tv1.getText() + strLine + "\n");
}
else{
tv1.setText(tv1.getText() + "nakaam" + "\n");
}
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
responseText.setText("MySampleFile.java data retrieved from external Storage...");
break;
}
tv1.setText(tv1.getText() + myData + "\n"+"\n"+"\n"+"end");
//tv1.setText(tv1.getText() + strLine + "\n");
}
据我所知意图关键字是每个文件静态的,所以我能得到这样的意图,但如何阅读这条线和大型文件只剩下一行一行的开始..?
as far my knowledge Intent keyword is static in every file so i can get the start of line like "Intent" but how to read this line and the next line only from large file..?
推荐答案
如果您想在关键字意图开始您的字符串,并阅读后线的数量,你可以做这样的事情:
If you want to start your String at the Keyword Intent, and read a Number of Lines after that, you can do something like this:
try {
BufferedReader br = new BufferedReader(new FileReader(file));
int remainingLines = 0;
String stringYouAreLookingFor = "";
for(String line; (line = br.readLine()) != null; ) {
if (line.startsWith("Intent")) {
remainingLines = <Number of Lines you want to read after keyword>;
stringYouAreLookingFor += line
} else if (remainingLines > 0) {
remainingLines--;
stringYouAreLookingFor += line
}
}
}
我想这应该给你如何找到你要查找的字符串的想法。
I guess this should give you an idea on how to search the String you are looking for.
这篇关于如何读取java文件中的特定行并打印出...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!