打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
TestLog

package com.aiatss.coast.common.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestLog {

    public static void main(String[] args) {
        for(int i=0;i<2;i++){
            test("898989");
        }
    }
    
    public static void test(String str){
       
        String path01=( new SimpleDateFormat("HH时mm分ss秒") ).format(new Date(System.currentTimeMillis()));
        String path02=( new SimpleDateFormat("HH时mm分") ).format(new Date(System.currentTimeMillis()));
        String path03=( new SimpleDateFormat("HH时") ).format(new Date(System.currentTimeMillis()));
        String path04=( new Date() ).getTime() +"";
       
        String logPath="d:\\temp_log\\temp log "+path02+".txt";
       
        // 编辑要写入的内容
        String temp="";
        if(str==null) temp="str is null";
        else temp=str;
        
        FileWriter writer;
        try {
            // 如果没有文件夹,创建文件夹
            File folder = new File("d:\\temp_log");
            if(!folder.exists()){
                folder.mkdirs();
            }
            // 创建txt文件,如果没有,自动创建,如果有,不覆盖
            File file = new File(logPath);
            if(file.isFile() && file.exists()){
                //System.out.println("文件存在");
            }else{
                file.createNewFile();
            }
            
            BufferedWriter bufw =  new BufferedWriter(new OutputStreamWriter(new FileOutputStream(logPath, true), "UTF-8"));
            
            bufw.write(path01);
            bufw.newLine();
            
            bufw.write(temp);
            bufw.newLine();
            
            bufw.flush();
            bufw.close();
           
        } catch (Exception e) {
        }finally{
            System.err.println(path01);
            readTxtFile(logPath);
        }
    }
    
    public static void readTxtFile(String filePath){
        try {
            String encoding="UTF-8";
            File file=new File(filePath);
            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                new FileInputStream(file),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine()) != null){
                    System.out.println(lineTxt);
                }
                read.close();
        }else{
            System.out.println("找不到指定的文件");
        }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
    }

}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java中io操作小例子
Java_功能实现_WordCount
java IO 流操作示例
基于Tcp协议的简单Socket通信实例(JAVA)
java socket 实现服务端与客户端
在java中如何用键盘输入一个数,字符,字符串
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服