All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.code.mojo.license.util.FileUtils Maven / Gradle / Ivy

There is a newer version: 1.10.b1
Show newest version
/**
 * Copyright (C) 2008 http://code.google.com/p/maven-license-plugin/
 *
 * 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.
 */

package com.google.code.mojo.license.util;

import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.InterpolationFilterReader;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.util.Map;

/**
 * Date: 16-Feb-2008
* Author: Mathieu Carbou ([email protected]) */ public final class FileUtils { private FileUtils() { } public static void write(File file, String content, String encoding) throws IOException { FileChannel channel = new FileOutputStream(file).getChannel(); try { channel.write(ByteBuffer.wrap(content.getBytes(encoding))); } finally { channel.close(); } } public static String read(URL location, Map properties) throws IOException { Reader reader = new InterpolationFilterReader(new BufferedReader(new InputStreamReader(location.openStream())), properties); try { return IOUtil.toString(reader); } finally { reader.close(); } } public static String read(File file, String encoding) throws IOException { FileChannel in = new FileInputStream(file).getChannel(); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); in.transferTo(0, in.size(), Channels.newChannel(baos)); return baos.toString(encoding); } finally { in.close(); } } public static String readFirstLines(File file, int lineCount, String encoding) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding)); try { String line; StringBuilder sb = new StringBuilder(); while (lineCount > 0 && (line = reader.readLine()) != null) { lineCount--; sb.append(line).append("\n"); } return sb.toString(); } finally { reader.close(); } } public static String remove(String str, String... chars) { for (String s : chars) { str = str.replace(s, ""); } return str; } public static void copyFileToFolder(File file, File folder) throws IOException { File dest = new File(folder, file.getName()); FileChannel inChannel = new FileInputStream(file).getChannel(); FileChannel outChannel = new FileOutputStream(dest).getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy