java如何从后缀为log的文件中提取数据?

java如何从后缀为log的文件中提取数据?,第1张

public static void main(String[] args) {

String fromFile = System.getProperty("user.dir") + File.separatorChar + "user.info"

String toFile = System.getProperty("user.dir") + File.separatorChar + "user1.info"

Reader r = null

Writer w = null

try {

r = new FileReader(fromFile)

w = new FileWriter(toFile)

} catch (IOException e) {

System.out.println(e.getMessage())

System.exit(0)

}

BufferedReader br = new BufferedReader(r)

BufferedWriter bw = new BufferedWriter(w)

// int s = -1

String line = null

try {

// while ((s = br.read()) != -1) {

//System.out.print((char)s)

//bw.write(s)

// }

while ((line = br.readLine()) != null) {

System.out.println(line)

bw.write(line)

bw.newLine()

//bw.write("\t\n")

}

bw.flush()

} catch (IOException e) {

System.out.println(e.getMessage())

} finally {

try {

bw.close()

} catch (IOException e) {

System.out.println(e.getMessage())

}

try {

br.close()

} catch (IOException e) {

System.out.println(e.getMessage())

}

}

}

看不看得懂就是你的事了,不做解答。

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()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11838949.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存