Here you can find the source of closeFile(File file, boolean toBeDeleted)
Parameter | Description |
---|---|
file | file to be closed |
toBeDeleted | flag to indicate if file needs to be deleted |
Parameter | Description |
---|---|
IOException | when failed to close the file handle |
public static void closeFile(File file, boolean toBeDeleted) throws IOException
//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(); } } } }