开发者

Saving a video file by increamenting file name in a loop

开发者 https://www.devze.com 2022-12-21 16:02 出处:网络
I wa开发者_Go百科nt to save a video file in C:\\ by incrementing the file name e.g. video001.avi video002.avi video003.avi etc. i want to do this in java. The program is on

I wa开发者_Go百科nt to save a video file in C:\ by incrementing the file name e.g. video001.avi video002.avi video003.avi etc. i want to do this in java. The program is on

Problem in java programming on windows7 (working well in windows xp)

How do i increment the file name so that it saves without replacing the older file.


Using the File.createNewFile() you can atomically create a file and determine whether or not the current thread indeed created the file, the return value of that method is a boolean that will let you know whether or not a new file was created or not. Simply checking whether or not a file exists before you create it will help but will not guarantee that when you create and write to the file that the current thread created it.


You have two options:

  1. just increment a counter, and rely on the fact that you're the only running process writing these files (and none exist already). So you don't need to check for clashes. This is (obviously) prone to error.
  2. Use the File object (or Apache Commons FileUtils) to get the list of files, then increment a counter and determine if the corresponding file exists. If not, then write to it and exit. This is a brute force approach, but unless you're writing thousands of files, is quite acceptable performance-wise.
0

精彩评论

暂无评论...
验证码 换一张
取 消