
正文
读取日志的java代码 读取日志是什么意思
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
怎么用java读取windows系统日志
您好读取日志的java代码,很高兴为您解答。
获取相关信息读取日志的java代码的简单的方法
import java.io.*;
import com.sun.management.OperatingSystemMXBean;
import sun.management.ManagementFactory;
public class Tst{
publicstatic String pt="D:\\abc.txt";
publicTst(){
}
public static void main(String[] args) throws Exception{
//free和use和total均为KB
longfree=0;
longuse=0;
longtotal=0;
intkb=1024;
Runtimert=Runtime.getRuntime();
total=rt.totalMemory();
free=rt.freeMemory();
use=total-free;
System.out.println("系统内存已用的空间为:"+use/kb+"MB");
System.out.println("系统内存的空闲空间为:"+free/kb+"MB");
System.out.println("系统总内存空间为:"+total/kb+"MB");
OperatingSystemMXBeanosmxb = (OperatingSystemMXBean)
ManagementFactory.getOperatingSystemMXBean();
longphysicalFree=osmxb.getFreePhysicalMemorySize()/kb;
longphysicalTotal=osmxb.getTotalPhysicalMemorySize()/kb;
longphysicalUse=physicalTotal-physicalFree;
Stringos=System.getProperty("os.name");
System.out.println("操作系统的版本:"+os);
System.out.println("系统物理内存已用的空间为:"+physicalFree+"MB");
System.out.println("系统物理内存的空闲空间为:"+physicalUse+"MB");
System.out.println("总物理内存:"+physicalTotal+"MB");
// 获得线程总数
ThreadGroup parentThread;
for(parentThread = Thread.currentThread().getThreadGroup();
parentThread
.getParent() != null; parentThread=
parentThread.getParent())
;
inttotalThread = parentThread.activeCount();
System.out.println("获得线程总数:"+totalThread);
}
}
// 3.操作系统信息
// a)取到当前操作系统的名称:
public String getPlatformName() {
String hostname = "";
try
{
hostname = InetAddress.getLocalHost().getHostName();
} catch
(Exception exc) {
Sigar sigar = new Sigar();
try {
hostname =
sigar.getNetInfo().getHostName();
} catch (SigarException e)
{
hostname = "localhost.unknown";
} finally
{
sigar.close();
}
}
return hostname;
}
// b)取当前操作系统的信息
public void testGetOSInfo() {
OperatingSystem OS =
OperatingSystem.getInstance();
// 操作系统内核类型如:
386、486、586等x86
System.out.println("OS.getArch() = " +
OS.getArch());
System.out.println("OS.getCpuEndian() = "
+OS.getCpuEndian());//
System.out.println("OS.getDataModel() = "
+OS.getDataModel());//
// 系统描述
System.out.println("OS.getDescription()
= " +OS.getDescription());
System.out.println("OS.getMachine() = "
+OS.getMachine());//
// 操作系统类型
System.out.println("OS.getName() = " +
OS.getName());
System.out.println("OS.getPatchLevel() = "
+OS.getPatchLevel());//
// 操作系统的卖主
System.out.println("OS.getVendor()
= " + OS.getVendor());
//
卖主名称
System.out
.println("OS.getVendorCodeName() = "
+OS.getVendorCodeName());
//
操作系统名称
System.out.println("OS.getVendorName() = "
+OS.getVendorName());
//
操作系统卖主类型
System.out.println("OS.getVendorVersion() = "
+OS.getVendorVersion());
//
操作系统的版本号
System.out.println("OS.getVersion() = "
+OS.getVersion());
}
// 4.资源信息(主要是硬盘)
//
a)取硬盘已有的分区及其详细信息(通过sigar.getFileSystemList()来获得FileSystem列表对象读取日志的java代码,然后对其进行编历):
public
void testFileSystemInfo() throws Exception {
Sigar sigar = new
Sigar();
FileSystem fslist[] = sigar.getFileSystemList();
//String dir
= System.getProperty("user.home");// 当前用户文件夹路径
for (int i = 0; i
fslist.length; i++) {
System.out.println("\n~~~~~~~~~~" + i
+"~~~~~~~~~~");
FileSystem fs = fslist[i];
//
分区的盘符名称
System.out.println("fs.getDevName() = "
+fs.getDevName());
// 分区的盘符名称
System.out.println("fs.getDirName() =
" +fs.getDirName());
System.out.println("fs.getFlags() = " +
fs.getFlags());//
// 文件系统类型读取日志的java代码,比如
FAT32、NTFS
System.out.println("fs.getSysTypeName() = "
+fs.getSysTypeName());
//
文件系统类型名,比如本地硬盘、光驱、网络文件系统等
System.out.println("fs.getTypeName() = "
+fs.getTypeName());
// 文件系统类型
System.out.println("fs.getType() = "
+fs.getType());
FileSystemUsage usage = null;
try {
usage =
sigar.getFileSystemUsage(fs.getDirName());
} catch (SigarException e)
{
if (fs.getType() == 2)
throw
e;
continue;
}
switch (fs.getType()) {
case 0: //
TYPE_UNKNOWN :未知
break;
case 1: //
TYPE_NONE
break;
case 2: // TYPE_LOCAL_DISK : 本地硬盘
//
文件系统总大小
System.out.println(" Total = " +usage.getTotal() +
"KB");
// 文件系统剩余大小
System.out.println(" Free = " +usage.getFree()
+ "KB");
// 文件系统可用大小
System.out.println(" Avail = "
+usage.getAvail() + "KB");
// 文件系统已经使用量
System.out.println(" Used
= " +usage.getUsed() + "KB");
double usePercent = usage.getUsePercent() *
100D;
// 文件系统资源的利用率
System.out.println(" Usage = " + usePercent
+"%");
break;
case 3:// TYPE_NETWORK :网络
break;
case
4:// TYPE_RAM_DISK :闪存
break;
case 5:// TYPE_CDROM
:光驱
break;
case 6:// TYPE_SWAP
:页面交换
break;
}
System.out.println(" DiskReads = "
+usage.getDiskReads());
System.out.println(" DiskWrites = "
+usage.getDiskWrites());
}
return;
}
如若满意,请点击右侧【采纳答案】,如若还有问题,请点击【追问】
希望读取日志的java代码我的回答对您有所帮助,望采纳!
~ O(∩_∩)O~
相关问答
Q1: java定时读取日志文件
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.ByteArrayInputStream;
class Reader implements Runnable {
Reader(String filename) {
this.filename = filename;
}
private String filename;
private long filelength = 0;
private int count=0;
@Override
public void run() {
while (true) {
try {
File f = new File(filename);
long nowlength = f.length();
long readlength = nowlength - filelength;
if (readlength == 0) {
Thread.sleep(1000);
continue;
}
RandomAccessFile rf = new RandomAccessFile(f, "r");
// 移动文件指针到上次读的最后
rf.seek(filelength);
filelength=nowlength;
byte[] b = new byte[(int) readlength];
rf.read(b, 0, b.length);
rf.close();
BufferedReader br=new BufferedReader(new InputStreamReader(new ByteArrayInputStream(b)));
String str=null;
count++;
System.out.println("第"+count+"次读到的内容:");
while((str=br.readLine())!=null){
System.out.println(str);
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Writer implements Runnable{
Writer(String filename) {
this.filename = filename;
}
private String filename;
private int count=0;
@Override
public void run() {
while (count++100){
try {
PrintWriter pw=new PrintWriter(new FileWriter(filename,true));
pw.append(""+count).append("\t").append(""+System.currentTimeMillis()).append("写入的内容").append("\r\n");
pw.close();
Thread.sleep(100);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class ReadFileFromTrail {
public static void main(String[] args) {
Reader reader=new Reader("d:\\test.log");
Writer writer=new Writer("d:\\test.log");
new Thread(reader).start();
new Thread(writer).start();
}
}
Q2: Java 如何获取控制台日志输出的信息
很简单的,如果你查api文档会发现类System有个“字段摘要”,很容易发现有个out,
它返回static PrintStream,还会发现System有个方法是static void setOut(PrintStream out)
重新分配“标准”输出流。 再点击PrintStream,很明显它是OutputStream
的子类 解决如下
输出流重定向
import java.io.*;
public class IO2File {
public static void main(String[] args) throws IOException {
File f=new File("out.txt");
f.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(f);
PrintStream printStream = new PrintStream(fileOutputStream);
System.setOut(printStream);
System.out.println("默认输出到控制台的这一句,输出到了文件 out.txt");
}
}
Q3: java怎么读取windows 事件日志 文件
您好,很高兴为您解答。日志文件在哪个目录,用fileinputstream读文件就行了
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class FileReaderTest {
static ListString logs = new ArrayListString();
public static ListString readFile(File f) throws IOException {
if (f == null || !f.exists()) {
return null;
}
ListString list = new ArrayListString();
String encoding = "UTF-8";
InputStreamReader read = new InputStreamReader(new FileInputStream(f),
encoding);
BufferedReader br = new BufferedReader(read);
String line = null;
读取日志的java代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于读取日志是什么意思、读取日志的java代码的信息别忘了在本站进行查找喔。







