title: Writing String to text files in a Rolling fashion Java
body: <p>I have a spring boot service which writes data from DB as a JSON(string) to text files.</p>
  <p>Since, this goes on continuously 24X7, I want to do this in a rolling fashion as in when size limit of a file is reached, it should create new file, and also create new directory for each date.</p>
  <p>I have used logback-spring.xml configuration to write the logs to files in a rolling fashion as described above, but I can't find any library or framework that can write JSON(string) to a text file in such fashion and not only logs.</p>
  <p>Can I use logback configuration to write both logs and the JSON content to text files in a rolling out fashion or is there any other library that I can use or I have to manually code the rolling process in my java file?</p>
  <p><strong>Edit:</strong>
  So, I want to rotate the log files as defined in logback-spring.xml configuration but I also want to rotate the text files that I'm writing to some other location.</p>
  <p><strong>Edit 2:</strong>
  Okay, so I have data in 3 tables: table1, table2, table3. I'm reading the data from these tables and writing to a text file using below code:</p>
  <pre><code>private void processEntry(Map&lt;String, Object&gt; entry) {
  
      FileWriter writer;
      Gson gson = new Gson();
      File f = new File(CALL_DATA_FILE_PATH);
  
      try {
          if (f.exists())
              writer = new FileWriter(CALL_DATA_FILE_PATH, true);
          else
              writer = new FileWriter(CALL_DATA_FILE_PATH);
  
          
          if (f.exists()) {
              writer.append(gson.toJson(entry) + &quot;,&quot;);
              log.info(&quot;File exists, appending data to it&quot;);
          } else {
              writer.write(gson.toJson(entry) + &quot;, \n&quot;);
              log.info(&quot;File does not exist, writing data to new file&quot;);
          }
          writer.flush();
  
          
      } catch (Exception e) {
          e.printStackTrace();
      }
  
  }
  </code></pre>
  <p>Now, I want to write the data and rotate the files based on date and time and don't want to manually handle that. Can this be achieved using logback configuration even if logback is currently used to write logs to specified location.</p>
  <p><strong>logback-spring.xml:</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;configuration&gt;
  &lt;property name=&quot;LOGS&quot; value=&quot;./logs&quot; /&gt;
  
  &lt;appender name=&quot;Console&quot;
      class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
      &lt;layout class=&quot;ch.qos.logback.classic.PatternLayout&quot;&gt;
          &lt;Pattern&gt;
              %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} -
              %msg%n
          &lt;/Pattern&gt;
      &lt;/layout&gt;
  &lt;/appender&gt;
  
  &lt;appender name=&quot;RollingFile&quot;
      class=&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;&gt;
      &lt;file&gt;${LOGS}/logs.log&lt;/file&gt;
      &lt;encoder
          class=&quot;ch.qos.logback.classic.encoder.PatternLayoutEncoder&quot;&gt;
          &lt;Pattern&gt;%d %p %C{1.} [%t] %m%n&lt;/Pattern&gt;
      &lt;/encoder&gt;
  
      &lt;rollingPolicy
          class=&quot;ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy&quot;&gt;
          &lt;fileNamePattern&gt;${LOGS}/archived/%d{yyyy-MM-dd}/logs-%d{yyyy-MM-dd}.%i.log
          &lt;/fileNamePattern&gt;
          &lt;maxFileSize&gt;10KB&lt;/maxFileSize&gt;
          &lt;maxHistory&gt;30&lt;/maxHistory&gt;
          &lt;totalSizeCap&gt;100KB&lt;/totalSizeCap&gt;
          &lt;cleanHistoryOnStart&gt;false&lt;/cleanHistoryOnStart&gt;
                  
      &lt;/rollingPolicy&gt;
  &lt;/appender&gt;
  
  &lt;root level=&quot;trace&quot;&gt;
      &lt;appender-ref ref=&quot;Console&quot; /&gt;
      &lt;appender-ref ref=&quot;RollingFile&quot; /&gt;
  &lt;/root&gt;
  &lt;/configuration&gt;
  </code></pre>