Java Path File Name nio getPrintWriter(String className, String flushPath)

Here you can find the source of getPrintWriter(String className, String flushPath)

Description

get Print Writer

License

Open Source License

Declaration

public static PrintWriter getPrintWriter(String className, String flushPath) 

Method Source Code


//package com.java2s;
/*/*  w ww  .  ja  v  a 2  s. c  om*/
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.Charset;

public class Main {
    public static PrintWriter getPrintWriter(String className, String flushPath) {
        File root = new File(flushPath);
        String path = root.getAbsolutePath() + File.separator + className + ".java";
        File classFile = prepareFile(path);
        try {
            return new PrintWriter(
                    new OutputStreamWriter(new FileOutputStream(classFile), Charset.defaultCharset()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static File prepareFile(String path) {
        File classFile = new File(path);
        if (!classFile.exists()) {
            try {
                File parent = classFile.getParentFile();
                if (!parent.exists()) {
                    parent.mkdirs();
                }
                classFile.createNewFile();
            } catch (IOException e) {
                System.out.println("Can't create file: " + path);
                System.out.println(e.getMessage());
            }
        }
        return classFile;
    }
}

Related

  1. GetPathName(String path)
  2. getPathOfClass(Class cls, String filename)
  3. getPathProperty(Properties props, String propName)
  4. getPaths(Path dir, String fileNames)
  5. getPrefix(final Path file, final int filenameSuffixLength)
  6. getProvenanceLogFiles(final String baseName, final Collection allProvenanceLogs)
  7. getQualifiedName(String outputDir, Path path)
  8. getRelativePathFile(String fileName)
  9. getRelativePathName(Path root, Path path)