每天创建一个新文件

时间:2014-07-30 14:11:46

标签: java android

我正在开发一个循环几个小时甚至几天的应用程序,我想要的是它循环过去当天到下一个它创建一个带有新文件名的新文件。 任何人都可以帮我逻辑吗? 这是我的代码:

    timedata= new File(timedata)
    String timedatafile=  "/sdcard/timedata"+"-"+formattedDate+".csv";

    if(MsureData.exists()!=true)
{
    timedata.createNewFile();
    Calendar c = Calendar.getInstance();
    String formattedDate = df.format(c.getTime()); 
    // do stuff
}

6 个答案:

答案 0 :(得分:0)

将当前日期保存在变量中并在循环中包含c.getTime(),当它与当前日期不同时,只需替换变量中的值并创建一个新文件。

//new date object initialized to current date
Date currentDate = new Date(); 
Calendar c = Calendar.getInstance();
//loop indefinately
while(true){
    if(!c.getTime().equals(currentDate)){
        //create a new file and set the current date
        currentDate = c.getTime();
    }
}

答案 1 :(得分:0)

您仍未提供足够的背景信息。您发布的代码甚至无法编译。

  • formatedDate在使用后定义
  • 您使用timedata作为File构造函数的参数和生成的新实例。没有File构造函数需要一个File实例(Source)。

但是,根据你所拥有的,我猜想:

一旦确定必须创建新实例,就不会创建新的File实例。您在获取新日期之前创建timedata实例并调用createNewFile()。确定要使用的日期后,才能创建File实例并创建文件。只更新formattedDate中的值不会更改现有File实例的文件名。

答案 2 :(得分:0)

每次将测量数据写入csv文件时,都要计算当前文件名并使用它。

假设您有某种服务,不时被计时器或类似程序调用,那么它的入口点是onSomeEvent,它会找出要使用的文件,然后对其执行某些操作。

class Writer extends SomeService {
    File current = null;
    DateFormat df = ...;

    public void onSomeEvent() {
        determineCurrentFile();
        // you can be sure that this file exists and it may or may not contain data already,
        // but for sure it is for the current date (today)
        AnotherClass.doSomethingWithMyFile(current);
    }

    private void determineCurrentFile() {
        File newFile = new File(getFileName());
        if(current == null) { // first usage
            current = newFile;
        } else if(!this.current.equals(newFile)) {
            current = newFile; // new day (doesn't matter how many days passed)
        } else {
            // same day, don't touch current
        }
        ensureFileExists(); // not sure if needed, I would let whoever writing it do the creation
    }

    private String getFileName() {
        Calendar c = Calendar.getInstance();
        String formattedDate = df.format(c.getTime());
        return formattedDate;
    }

    private void ensureFileExists() {
        if(current == null) {
            throw new IllegalStateException("determineFile should have filled this.current");
        }
        if(!current.exists()) {
            current.createNewFile();
        }
    }
}

答案 3 :(得分:0)

您可以使用Quartz API来安排在特定时间执行的作业。请参阅这些教程 - http://www.mkyong.com/java/quartz-scheduler-example/

答案 4 :(得分:0)

int yourdelay=86400000; // delay in millisecond, 24 hours=86400000 millisecond

final Handler handler = new Handler();

handler.post(new Runnable()
{
    @Override
    public void run()
    {
       Calendar c = Calendar.getInstance();
       SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.getDefault());
       String formattedDate = df.format(c.getTime());

       String pathfile=  "/sdcard/timedata"+"-"+formattedDate+".csv";
       File filedata= new File(pathfile);

       //if file not exists create new file
       if(!filedata.exists())
       {
          filedata.createNewFile();                

       }

      handler.postDelayed(this, yourdelay);

    }
});

这一个表单可以解决您在同一小时和分钟内逐日运行处理程序的问题。

答案 5 :(得分:-1)

Date currentDate = new Date(System.currentTimeMillis());

从此可以获得系统的当前日期。

相关问题