Java Temp File Create newTmpFile(String content, String encoding)

Here you can find the source of newTmpFile(String content, String encoding)

Description

Makes a new temporary file and writes content into it.

License

Open Source License

Parameter

Parameter Description
content Initial content of the temporary file.
encoding Encoding to use.

Exception

Parameter Description
IOException If the content creation failed.

Return

The handle to the new temporary file

Declaration

public static File newTmpFile(String content, String encoding) throws IOException 

Method Source Code


//package com.java2s;
/* //from   ww  w .  j a va 2 s .co m
JSPWiki - a JSP-based WikiWiki clone.
    
Copyright (C) 2001 Janne Jalkanen (Janne.Jalkanen@iki.fi)
    
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
    
This program 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 Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.*;

public class Main {
    /**
     *  Makes a new temporary file and writes content into it.
     *
     *  @param content Initial content of the temporary file.
     *  @param encoding Encoding to use.
     *  @return The handle to the new temporary file
     *  @throws IOException If the content creation failed.
     */
    public static File newTmpFile(String content, String encoding) throws IOException {
        Writer out = null;
        Reader in = null;
        File f = null;

        try {
            f = File.createTempFile("jspwiki", null);

            in = new StringReader(content);

            out = new OutputStreamWriter(new FileOutputStream(f), encoding);

            copyContents(in, out);
        } finally {
            if (in != null)
                in.close();
            if (out != null)
                out.close();
        }

        return f;
    }

    /** Default encoding is ISO-8859-1 */
    public static File newTmpFile(String content) throws IOException {
        return newTmpFile(content, "ISO-8859-1");
    }

    /**
     *  Just copies all characters from <I>in</I> to <I>out</I>.
     *
     *  @since 1.5.8
     */
    // FIXME: Could probably be more optimized
    public static void copyContents(Reader in, Writer out) throws IOException {
        int c;

        while ((c = in.read()) != -1) {
            out.write(c);
        }

        out.flush();
    }

    /**
     *  Just copies all characters from <I>in</I> to <I>out</I>.
     *
     *  @since 1.9.31
     */
    // FIXME: Could probably be more optimized
    public static void copyContents(InputStream in, OutputStream out) throws IOException {
        int c;

        while ((c = in.read()) != -1) {
            out.write(c);
        }

        out.flush();
    }
}

Related

  1. getUniqueFilename(String filename, String extension)
  2. getUniqueFileName(String fileNamePrefix, String fileExtension)
  3. getUniqueFileName(String originalFileName)
  4. getUniqueTempFile(final boolean autoDelete, final File parentFolder, final String suffix)
  5. newTmpFile(String content)