如何用java读取txt中的java文件,并且记录其中char,int,if,while等出现的个数 200
1个回答
展开全部
读取java文件就要用到io了,每一个单词应该会有分隔符,可能是空格,逗号import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File file = new File(""/** 文件名 */
);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String sb = "";
int count = 0;
sb = br.readLine();
while (sb != null) {
String[] array = sb.split(" "/** 文件分隔符 */
);
for (String string : array) {
if (string.equals("while")) {
count++;
}
}
sb = br.readLine();
}
System.out.println(count);
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File file = new File(""/** 文件名 */
);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String sb = "";
int count = 0;
sb = br.readLine();
while (sb != null) {
String[] array = sb.split(" "/** 文件分隔符 */
);
for (String string : array) {
if (string.equals("while")) {
count++;
}
}
sb = br.readLine();
}
System.out.println(count);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询