Description
Creates a new temporary file using the default encoding of ISO-8859-1 (Latin1).
License
Apache License
Parameter
Parameter | Description |
---|
content | The content to put into the file. |
Exception
Parameter | Description |
---|
IOException | If writing was unsuccessful. |
Return
A handle to the newly created file.
Declaration
public static File newTmpFile(String content) throws IOException
Method Source Code
//package com.java2s;
/*//from w ww . j a v a 2 s . c o m
* Copyright (c) 2008-2016 Computer Network Information Center (CNIC), Chinese Academy of Sciences.
*
* This file is part of Duckling project.
*
* Licensed 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.
*
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
public class Main {
/** Size of the buffer used when copying large chunks of data. */
private static final int BUFFER_SIZE = 4096;
/**
* Makes a new temporary file and writes content into it. The temporary
* file is created using <code>File.createTempFile()</code>, and the usual
* semantics apply. The files are not deleted automatically in exit.
*
* @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.
* @see java.io.File#createTempFile(String,String,File)
*/
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;
}
/**
* Creates a new temporary file using the default encoding
* of ISO-8859-1 (Latin1).
*
* @param content The content to put into the file.
* @throws IOException If writing was unsuccessful.
* @return A handle to the newly created file.
* @see #newTmpFile( String, String )
* @see java.io.File#createTempFile(String,String,File)
*/
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>. The copying
* is performed using a buffer of bytes.
*
* @since 1.5.8
* @param in The reader to copy from
* @param out The reader to copy to
* @throws IOException If reading or writing failed.
*/
public static void copyContents(Reader in, Writer out) throws IOException {
char[] buf = new char[BUFFER_SIZE];
int bytesRead = 0;
while ((bytesRead = in.read(buf)) > 0) {
out.write(buf, 0, bytesRead);
}
out.flush();
}
/**
* Just copies all bytes from <I>in</I> to <I>out</I>. The copying is
* performed using a buffer of bytes.
*
* @since 1.9.31
* @param in The inputstream to copy from
* @param out The outputstream to copy to
* @throws IOException In case reading or writing fails.
*/
public static void copyContents(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[BUFFER_SIZE];
int bytesRead = 0;
while ((bytesRead = in.read(buf)) > 0) {
out.write(buf, 0, bytesRead);
}
out.flush();
}
}
Related
- getUniqueFileName()
- getUniqueFilename(String filename, String extension)
- getUniqueFileName(String fileNamePrefix, String fileExtension)
- getUniqueFileName(String originalFileName)
- getUniqueTempFile(final boolean autoDelete, final File parentFolder, final String suffix)
- newTmpFile(String content, String encoding)