Description
Creates a new temporary file using the default encoding of ISO-8859-1 (Latin1).
License
Open Source 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
/*//from www.java2 s . c om
Copyright (c) Inexas 2010
Modifications licensed under the Inexas Software License V1.0. You
may not use this file except in compliance with the License.
The License is available at: http://www.inexas.com/ISL-V1.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.
The original file and contents are licensed under a separate license:
see below.
*/
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import org.apache.log4j.Logger;
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
- getTempFile(String dir, String fileExt)
- getTempFile(String prefix, String suffix)
- getTempFile(String tmpName)
- getTempImageFile(Context context)
- getTempVideoFile()
- newTmpFile(String content, String encoding)