Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.Iterator;
import java.util.Map;

public class Main {
    public static boolean addTemplateFile(InputStream src, File dest, Map replace) {
        try {
            FileOutputStream out = new FileOutputStream(dest);

            String outstr = patchTemplateFile(src, replace);
            out.write(outstr.getBytes());
            out.close();
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    public static String patchTemplateFile(InputStream src, Map replace) {
        InputStreamReader fr = new InputStreamReader(src);
        BufferedReader br = new BufferedReader(fr);

        StringBuffer result = new StringBuffer();
        try {
            String line;
            while ((line = br.readLine()) != null) {
                Iterator iter;
                if (replace != null) {
                    for (iter = replace.keySet().iterator(); iter.hasNext();) {
                        String key = (String) iter.next();
                        line = line.replaceAll(key, (String) replace.get(key));
                    }
                }
                result.append(line + "\n");
            }

            br.close();
            fr.close();
        } catch (IOException e) {
            return "";
        }

        return result.toString();
    }
}