Java Path File Name nio appendTargetToBuildFile(String targetName, Path dir)

Here you can find the source of appendTargetToBuildFile(String targetName, Path dir)

Description

append Target To Build File

License

Open Source License

Declaration

static void appendTargetToBuildFile(String targetName, Path dir) throws IOException 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.CREATE;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class Main {
    private static final String BUILD_FILE_NAME = "BUILD";

    static void appendTargetToBuildFile(String targetName, Path dir) throws IOException {
        appendLinesToFile(dir.resolve(BUILD_FILE_NAME), "cc_library(", "    name = '" + targetName + "',",
                "    srcs = [ '" + targetName + ".cc', '" + targetName + ".h' ],", ")");
    }//from w  w  w  . j av a 2s . c  om

    private static void appendLinesToFile(Path filePath, String... lines) throws IOException {
        writeOrAppendLinesToFile(true, filePath, lines);
    }

    private static void writeOrAppendLinesToFile(boolean append, Path filePath, String... lines)
            throws IOException {
        File file = filePath.toFile();
        if (!file.exists() && !file.createNewFile()) {
            return;
        }

        PrintWriter printWriter = new PrintWriter(Files.newBufferedWriter(file.toPath(), UTF_8,
                append ? new StandardOpenOption[] { CREATE, APPEND } : new StandardOpenOption[] { CREATE }));
        for (String line : lines) {
            printWriter.println(line);
        }
        printWriter.close();
    }
}

Related

  1. addPath(JarOutputStream outputStream, Path path, String entryName)
  2. addRestorePermissions(String username, Path file)
  3. checkFile(final String friendlyname, final Path file, final boolean isdirectory, final boolean canwrite)
  4. compileAndLoad(Path basePath, String className)
  5. concatFileName(String fileName, Path pathLocation)
  6. convertFilePathToName(Path file)