在java中1小时后更新csv文件

时间:2017-03-12 17:07:53

标签: java netbeans-8

我在java中创建了一个线程,它在1小时的时间间隔后不断检查窗口中的最近项目,并生成所有最近项目的.csv文件。此外,我有最近项目中所有文件的访问时间和文件夹位置。现在我想要做的是因为最近的项目不断更新自己,所以如果最近的项目中有一个新文件,它应该附加在已经制作的csv文件的末尾但是我被困在这里并且不知道如何做到这一点请帮助我,我的代码在这里

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package record;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import static java.sql.DriverManager.println;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.awt.shell.ShellFolder;

/**
 *
 * @author zeeshan
 */
public class Record 
{
    static String user=System.getProperty("user.name");
    static String path1="C:\\Users\\"+user+"\\AppData\\Roaming\\Microsoft\\Windows\\Recent\\";
    static String path2="C:\\Users\\Fa16Rcs028\\Dropbox\\Spring 17\\Special Topics In HCI\\Sir Aimal Project\\output.csv";
    static PrintWriter pw;
    static StringBuilder sb = new StringBuilder();
    public void createcsv()throws IOException
    {


            File directory = new File(path1);
            File[] fList = directory.listFiles();
            pw = new PrintWriter(new File(path2));

            sb.append("File/Folder Name");
            sb.append(',');
            sb.append("Access Time");
            sb.append(',');
            sb.append("File Location");
            sb.append('\n');
            for (int i=0;i<fList.length;i++)
            {
                String filename=fList[i].getName();
                String actualfilename=filename.replace(".lnk", "");
                ShellFolder sf = ShellFolder.getShellFolder(fList[i]);
                ShellFolder target = sf.getLinkLocation();
                if (target != null)
                {
                    Path p = Paths.get(path1+filename);
                    BasicFileAttributes view= Files.getFileAttributeView(p, BasicFileAttributeView.class).readAttributes();
                    FileTime fileTime=view.creationTime();
                    sb.append(actualfilename);
                    sb.append(',');
                    sb.append(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format((fileTime.toMillis())));
                    sb.append(',');
                    sb.append(target.getAbsolutePath());
                    sb.append('\n');



                }




            }
            pw.write(sb.toString());
            pw.close();

    }
    public static class maintainrecord extends Thread
    {
        @Override
        public void run()
        {
            File directory = new File(path1);
            File[] fList = directory.listFiles();
            for (File fList1 : fList) {
                try {
                    String filename = fList1.getName();
                    String actualfilename=filename.replace(".lnk", "");
                    Path p = Paths.get(path1+filename);
                    BasicFileAttributes view= Files.getFileAttributeView(p, BasicFileAttributeView.class).readAttributes();
                    FileTime fileTime=view.creationTime();
                    String time=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format((fileTime.toMillis())).toString();
                    String line = "";
                    String cvsSplitBy = ",";
                    try (BufferedReader br = new BufferedReader(new FileReader(path2)))
                    {
                        while ((line = br.readLine()) != null)
                        {
                            String[] record = line.split(cvsSplitBy);
                            String name=record[0];
                            String checktime=record[1];
                            if(actualfilename.equals(name) && !time.equals(checktime))
                            {
                                br.close();
                                pw = new PrintWriter(new File(path2));
                                sb.append(actualfilename);
                                sb.append(',');
                                sb.append(actualfilename);
                                sb.append(',');
                            }
                        }

                    }
                }catch (IOException ex)
                {
                    Logger.getLogger(Record.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

    }

    public static void main(String[] args) throws IOException 
    {
        Record r=new Record();
        r.createcsv();
        Thread zeeshan=new Thread(new  maintainrecord());
        zeeshan.start();
        Thread.sleep(600000);


    }



}

0 个答案:

没有答案