Skip to content

Commit

Permalink
move index files to prefixed and hidden files
Browse files Browse the repository at this point in the history
  • Loading branch information
bigboxer23 committed Dec 20, 2022
1 parent d4bf3ba commit 6f0a07d
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/main/java/com/bigboxer23/utils/FilePersistentIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
public class FilePersistentIndex
{
public static final String kPrefix = ".index_";

private static final Logger logger = LoggerFactory.getLogger(FilePersistentIndex.class);

private int currentIndex = -1;
Expand All @@ -22,14 +24,28 @@ public class FilePersistentIndex
public FilePersistentIndex(String fileName)
{
this.filePath = System.getProperty("user.dir") + File.separator + fileName;
currentIndex = getIndexFromFile(this.filePath);
if (currentIndex > -1)
{
new File(this.filePath).delete();
this.filePath = System.getProperty("user.dir") + File.separator + kPrefix + fileName;
writeToFile();
}
this.filePath = System.getProperty("user.dir") + File.separator + kPrefix + fileName;
currentIndex = getIndexFromFile(this.filePath);
logger.info(fileName + ": initialized with value " + currentIndex);
}

private static int getIndexFromFile(String path)
{
try
{
currentIndex = Integer.parseInt(FileUtils.readFileToString(new File(this.filePath), Charset.defaultCharset()).trim());
return Integer.parseInt(FileUtils.readFileToString(new File(path), Charset.defaultCharset()).trim());
} catch (NumberFormatException e)
{
logger.warn("FilePersistentIndex", e);
} catch (IOException e) {}
logger.info(fileName + ": initialized with value " + currentIndex);
return -1;
}

private void writeToFile()
Expand Down

0 comments on commit 6f0a07d

Please sign in to comment.