51,386
社区成员




public static void checkSystemLog(String logFile) {
//思路:读取文件内容,查询是否有相同的异常信息,查询到的信息输出到txt文件中
//1.声明一个输入流对象
FileInputStream fileInput = null;
File file = new File(logFile);
try {
// //2.通过file对象构建一个流对象
// fileInput = new FileInputStream(file);
// //3.设置字节数组,1024只是一个习惯
// byte[] data = new byte[1024];
// StringBuffer stb = new StringBuffer();
// int len;//用来存储read(byte[] b)方法的返回值,代表每次读入的字节个数;当因为到达文件末尾而没有字节读入时,返回-1
// while((len=fileInput.read(data))!=-1){
// stb.append(data);
// }
// String string = new String(stb.toString().getBytes(),"GBK");
// System.out.println("string"+string);
// 通过File对象构建一个流对象
fileInput = new FileInputStream(file);
// 读取数据,并将读取的数据存储在数组中
byte[] data = new byte[(int)file.length()];
// 读取流中的第一个字节数据
int n = fileInput.read();
// 读取的游标位置
int i = 0;
// 判断是否读到最后一个字符
while(n != -1) {
data[i] = (byte)n;
i++;
n = fileInput.read();
}
String rs = new String(data, "GBK");
System.out.println(rs);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//2.通过file对象构建一个流对象
fileInput = new FileInputStream(file);
//3.设置字节数组,1024只是一个习惯
byte[] data = new byte[1024];
StringBuffer stb = new StringBuffer();
int leng;//用来存储read(byte[] b)方法的返回值,代表每次读入的字节个数;当因为到达文件末尾而没有字节读入时,返回-1
while((leng=fileInput.read(data))!=-1){
stb.append(new String(data, 0, leng));
}
String string = new String(stb.toString().getBytes(),"GBK");
System.out.println("string"+string);