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

org.apache.empire.db.codegen.util.FileUtils Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.empire.db.codegen.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * File utilities
 * TODO would be better to let these methods throw IOExceptions!
 *
 */
public class FileUtils {
	
	private static final Logger log =  LoggerFactory.getLogger(FileUtils.class);
	public static final String SEPARATOR =  System.getProperty("file.separator");
	private FileUtils()
	{
		// Utility class
	}
	
	/**
	 * Creates a directory structure due to a java package name within the specified folder.
	 * @param target,	the target folder
	 * @param pack,		the java package name
	 */
	public static File getFileFromPackage(File target, String pack) {
		StringBuilder sb = new StringBuilder();
		sb.append(target.getAbsolutePath()).append(SEPARATOR);
		sb.append(pack.replace(".", SEPARATOR));
		File f = new File(sb.toString());
		if (!f.exists()) {
			f.mkdirs();
		}
		return f;
	}
	
	/**
	 * Recursively cleans (removes) all files under the given 
	 * directory.  Note that this removes all sub-directories
	 * but not the parent directory.  
	 * @param directory
	 */
	public static void cleanDirectory(File directory) {
		boolean success;
		if (directory.isDirectory()) 
		{
            String[] children = directory.list();
            for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy