Java Stream Close closeFile(File file, boolean toBeDeleted)

Here you can find the source of closeFile(File file, boolean toBeDeleted)

Description

Closes the file handle for temporary file.

License

Apache License

Parameter

Parameter Description
file file to be closed
toBeDeleted flag to indicate if file needs to be deleted

Exception

Parameter Description
IOException when failed to close the file handle

Declaration

public static void closeFile(File file, boolean toBeDeleted) throws IOException 

Method Source Code

//package com.java2s;
/*/*from   www  .jav a  2s  .  c o  m*/
 * Copyright 2016-present Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Closes the file handle for temporary file.
     *
     * @param file        file to be closed
     * @param toBeDeleted flag to indicate if file needs to be deleted
     * @throws IOException when failed to close the file handle
     */
    public static void closeFile(File file, boolean toBeDeleted) throws IOException {

        if (file != null) {
            updateFileHandle(file, null, true);
            if (toBeDeleted) {
                boolean deleted = file.delete();
                if (!deleted) {
                    throw new IOException("Failed to delete temporary file " + file.getName());
                }
            }
        }
    }

    /**
     * Closes the file handle for temporary file with file deletion.
     *
     * @param file file to be closed
     * @throws IOException when failed to close the file handle
     */
    public static void closeFile(File file) throws IOException {

        if (file != null) {
            updateFileHandle(file, null, true);
            boolean deleted = file.delete();
            if (!deleted) {
                throw new IOException("Failed to delete temporary file " + file.getName());
            }
        }
    }

    /**
     * Updates the generated file handle.
     *
     * @param inputFile        input file
     * @param contentTobeAdded content to be appended to the file
     * @param isClose          when close of file is called.
     * @throws IOException if the named file exists but is a directory rather than a regular file, does not exist but
     *                     cannot be created, or cannot be opened for any other reason
     */
    public static void updateFileHandle(File inputFile, String contentTobeAdded, boolean isClose)
            throws IOException {

        List<FileWriter> fileWriterStore = new ArrayList<>();

        FileWriter fileWriter = new FileWriter(inputFile, true);
        fileWriterStore.add(fileWriter);
        PrintWriter outputPrintWriter = new PrintWriter(fileWriter, true);
        if (!isClose) {
            outputPrintWriter.write(contentTobeAdded);
            outputPrintWriter.flush();
            outputPrintWriter.close();
        } else {
            for (FileWriter curWriter : fileWriterStore) {
                curWriter.flush();
                curWriter.close();
            }
        }
    }
}

Related

  1. closeDebugFile()
  2. closeEL(InputStream is)
  3. closeEntry(ZipOutputStream... outputStreams)
  4. closeFile()
  5. closeFile()
  6. closeFile(File file, RandomAccessFile raf)
  7. closeFile(FileWriter fw)
  8. closeFile(final Closeable p_closeable)
  9. closeFile(PrintStream ps)