SimpleFileWriter.java 1.8 KB
Newer Older
1
/*
2
 * The MIT License
3
 * Copyright © 2014-2021 Ilkka Seppälä
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
23

24
package com.iluwatar.execute.around;
25 26 27 28

import java.io.FileWriter;
import java.io.IOException;

29 30
import lombok.extern.slf4j.Slf4j;

31
/**
32 33
 * SimpleFileWriter handles opening and closing file for the user. The user only has to specify what
 * to do with the file resource through {@link FileWriterAction} parameter.
34
 */
35
@Slf4j
36 37
public class SimpleFileWriter {

38
  /**
39
   * Constructor.
40
   */
41
  public SimpleFileWriter(String filename, FileWriterAction action) throws IOException {
42
    LOGGER.info("Opening the file");
43
    try (var writer = new FileWriter(filename)) {
44
      LOGGER.info("Executing the action");
45
      action.writeFile(writer);
46
      LOGGER.info("Closing the file");
47 48
    }
  }
49
}