Java tutorial
/* * COPYRIGHT AND LICENSE * * Copyright 2014 The Regents of the University of California All Rights Reserved * * Permission to copy, modify and distribute any part of this cws-war for * educational, research and non-profit purposes, without fee, and without a * written agreement is hereby granted, provided that the above copyright notice, * this paragraph and the following three paragraphs appear in all copies. * * Those desiring to incorporate this cws-war into commercial products * or use for commercial purposes should contact the Technology Transfer Office, * University of California, San Diego, 9500 Gilman Drive, Mail Code 0910, * La Jolla, CA 92093-0910, Ph: (858) 534-5815, FAX: (858) 534-7345, * E-MAIL:invent@ucsd.edu. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING * LOST PROFITS, ARISING OUT OF THE USE OF THIS cws-war, EVEN IF THE UNIVERSITY * OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE cws-war PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND THE UNIVERSITY * OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, * ENHANCEMENTS, OR MODIFICATIONS. THE UNIVERSITY OF CALIFORNIA MAKES NO * REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER IMPLIED OR * EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR THAT THE USE OF * THE cws-war WILL NOT INFRINGE ANY PATENT, TRADEMARK OR OTHER RIGHTS. */ package edu.ucsd.crbs.cws.io; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.util.List; import org.apache.commons.io.IOUtils; /** * Reads in a resource and writes it out as a script with logic that enables * caller to adjust each line of text before it is written * * @author Christopher Churas <churas@ncmir.ucsd.edu> */ public class ResourceToExecutableScriptWriterImpl implements ResourceToExecutableScriptWriter { /** * Writes a given resource to an executable script file. The result is written to * <b>destinationScript</b> and this script is made executable via File setExecutable * method. * * @param resourcePath Path that can be loaded via {@link Class.class.getResourceAsStream} * @param destinationScript File to write script to * @param replacer Optional object that lets caller alter script on a line by line basis before it is written * @throws Exception if there is an io error * @throws IllegalArgumentException if either <b>resourcePath</b> or <b>destinationScript</b> are null */ @Override public void writeResourceToScript(final String resourcePath, final String destinationScript, StringReplacer replacer) throws Exception { if (resourcePath == null) { throw new IllegalArgumentException("resourcePath method parameter cannot be null"); } if (destinationScript == null) { throw new IllegalArgumentException("destinationScript method parameter cannot be null"); } //load script List<String> scriptLines = IOUtils.readLines(Class.class.getResourceAsStream(resourcePath)); BufferedWriter bw = new BufferedWriter(new FileWriter(destinationScript)); for (String line : scriptLines) { if (replacer != null) { bw.write(replacer.replace(line)); } else { bw.write(line); } bw.newLine(); } bw.flush(); bw.close(); //make script executable File script = new File(destinationScript); script.setExecutable(true, false); } }