import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Scanner;
public class WriteFile {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名");
String username = sc.next();
System.out.println("请输入密码");
String password = sc.next();
if (username.equals("admin") && password.equals("123456")) {
System.out.println("登录成功");
// 记录日志
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String logintime = sdf.format(new java.util.Date());
String logs = username + ":" + logintime;
// 写文件
writeLogs(logs);
}
}
//写日志
private static void writeLogs(String logs) {
//1 创建文件对象
File f = new File("c:\\logs.txt");
try {
//2 通过文件对象创建文件输出流
FileOutputStream fileout = new FileOutputStream(f);
//3 将要写入文件的字符串转化成字节
byte buf[] = logs.getBytes();
//4 将字节写入文件
fileout.write(buf);
//5 关闭输出流
fileout.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
import java.text.SimpleDateFormat;
import java.util.Scanner;
public class WriteFile {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名");
String username = sc.next();
System.out.println("请输入密码");
String password = sc.next();
if (username.equals("admin") && password.equals("123456")) {
System.out.println("登录成功");
// 记录日志
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String logintime = sdf.format(new java.util.Date());
String logs = username + ":" + logintime;
// 写文件
writeLogs(logs);
}
}
//写日志
private static void writeLogs(String logs) {
//1 创建文件对象
File f = new File("c:\\logs.txt");
try {
//2 通过文件对象创建文件输出流
FileOutputStream fileout = new FileOutputStream(f);
//3 将要写入文件的字符串转化成字节
byte buf[] = logs.getBytes();
//4 将字节写入文件
fileout.write(buf);
//5 关闭输出流
fileout.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}