Java File Append LIne appendLineToFile(String fileName, String format, Object... vals)

Here you can find the source of appendLineToFile(String fileName, String format, Object... vals)

Description

Appends a line to a file.

License

Open Source License

Parameter

Parameter Description
fileName Name of a file to append to.
format A line format.
vals A line format args.

Exception

Parameter Description
IOException If IO error occurs.

Declaration

public static void appendLineToFile(String fileName, String format, Object... vals) throws IOException 

Method Source Code


//package com.java2s;
/* //w w  w. j a  v a  2s  .  c  om
 Copyright (C) GridGain Systems. All Rights Reserved.
     
 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.*;

public class Main {
    /**
     * Appends a line to a file. Creates parent directories if any of those do not
     * exist.
     *
     * @param fileName Name of a file to append to.
     * @param format A line format.
     * @param vals A line format args.
     * @throws IOException If IO error occurs.
     */
    public static void appendLineToFile(String fileName, String format, Object... vals) throws IOException {
        new File(fileName).getParentFile().mkdirs();

        try (Writer out = new BufferedWriter(new FileWriter(fileName, true))) {
            out.write(String.format(format + '\n', vals));
        }
    }
}

Related

  1. appendLine(String path, String line)
  2. appendLineInFile(String path, String lines)
  3. appendLines(File file, String[] lines)
  4. appendLines(File file, String[] lines)
  5. appendLinesToFile(Iterable lines, File outFile)
  6. AppendLineToFile(String filePath, String text)
  7. appendLineToFile(String path, String line)
  8. appendLineToTextFile(File file, String line)