是否有另一个库可以允许写入文档(例如.log)'Date'类型,因为FileOutputStream不允许这样做?



我有以下代码和想要的信息,如日期,计数和true(日期,int和布尔类型)写入我的文件(例如atm.log)。此外,当我删除Date和布尔类型时,编译会继续进行,但文件最终没有计数。我不知道为什么当Date和布尔值被遗漏时不包括计数数,因为我有'close()'方法。

主:

import java.io.File;  // Import the File class
import java.io.FileNotFoundException;  // Import this class to handle errors
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
try {
File myObj = new File("atm.log");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
//while(true)
//{
ATM atm = new ATM();
atm.init();
atm.run();
//}
}
}

类:

import java.util.Date;
import java.util.ArrayList;
public class UserInfo
{
private final Date date;
private final int count;
private final boolean correct;
public UserInfo(Date date, int count, boolean correct)
{
this.date = date;
this.count = count;
this.correct = correct;
}
public Date getDate()
{
return date;
}
public int getCount()
{
return count;
}
public boolean isCorrect()
{
return correct;
}
}

类:

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.*;
import java.io.FileOutputStream;
public class Logger
{
ArrayList<UserInfo> obj = new ArrayList<>();
public Logger(Date date, int count, boolean correct){
//obj.get(0).setValue(date,count, correct);
obj.add(new UserInfo(date, count, correct));
try (FileOutputStream out = new FileOutputStream("atm.log")) {

for (UserInfo s : obj)//int i = 0; i < obj.size(); i++
{
out.write(s.getCount());
out.write(s.getDate());
out.write(s.isCorrect());
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}

}
}

错误:

java: no suitable method found for write(java.util.Date)
method java.io.FileOutputStream.write(int) is not applicable
(argument mismatch; java.util.Date cannot be converted to int)
method java.io.FileOutputStream.write(byte[]) is not applicable
(argument mismatch; java.util.Date cannot be converted to byte[])

您可以只写入字节流(如FileoutputSteam)。或者让Java完成生成字节的工作,只写字符——但在这种情况下,您需要使用Writer。你应该很熟悉printwwriter——你在System.out.println(…)

期间使用它
try (
OutputStream outstream = new FileOutputStream(...);
PrintWriter out = new PrintWriter(outstream);
) {
out.println(new Date());
}

一般来说,写入文件与格式无关,也就是说,由您决定。可以使用JSON、XML、CSV等来应用通用格式,但所有这些决定都增加了不同程度的复杂性。

因此,例如,一个简单的解决方案可能是定义一个人类可读的格式,例如…
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
Logger logger = new Logger();
logger.log(LocalDateTime.now(), 100, true);
logger.log(LocalDateTime.now(), 200, false);
logger.log(LocalDateTime.now(), 300, true);
logger.log(LocalDateTime.now(), 400, false);
logger.log(LocalDateTime.now(), 500, true);
logger.dump();
}
public class Logger {
private ArrayList<UserInfo> obj = new ArrayList<>();
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd-HH:mm:ss");
public Logger() {
}
public void log(LocalDateTime date, int count, boolean correct) {
UserInfo info = new UserInfo(date, count, correct);
obj.add(info);
// You could keep the file open until the class in closed
try (BufferedWriter bw = new BufferedWriter(new FileWriter("atm.log", true))) {
String text = String.format("[%s] %4d %s", formatter.format(info.getDate()), info.getCount(), info.isCorrect() ? "Yes" : "No");
bw.write(text);
bw.newLine();;
} catch (IOException e) {
e.printStackTrace();
}
}
public void dump() {
try (BufferedReader br = new BufferedReader(new FileReader("atm.log"))) {
String text = null;
while ((text = br.readLine()) != null) {
System.out.println(text);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class UserInfo {
private final LocalDateTime date;
private final int count;
private final boolean correct;
public UserInfo(LocalDateTime date, int count, boolean correct) {
this.date = date;
this.count = count;
this.correct = correct;
}
public LocalDateTime getDate() {
return date;
}
public int getCount() {
return count;
}
public boolean isCorrect() {
return correct;
}
}
}

它将打印类似于…

[2021/10/08-08:46:15]  100 Yes
[2021/10/08-08:46:15]  200 No
[2021/10/08-08:46:15]  300 Yes
[2021/10/08-08:46:15]  400 No
[2021/10/08-08:46:15]  500 Yes

一些笔记…

我使用LocalDateTime作为Date的首选项,我们现在应该做的事情,但基本概念将与DateSimpleDateFormatter一起工作。

这是没有意义的,我需要创建一个新的Logger实例来添加一个新的日志条目,然后被添加到List…并且每个Logger实例都覆盖前一个文件。

因此,我创建了Logger来支持多个日志输出,并且每次都附加到文件中。

最新更新