Description
Write out a file to the filename specified.
License
Open Source License
Parameter
Parameter | Description |
---|
filename | filename to write to |
contents | file contents |
Exception
Parameter | Description |
---|
FileNotFoundException | if the file does not exist |
IOException | if there is a problem writing |
Declaration
public static void writeFile(String filename, String contents) throws FileNotFoundException, IOException
Method Source Code
//package com.java2s;
/*//from w w w . ja va 2 s .c o m
* Copyright 1997-2016 Unidata Program Center/University Corporation for Atmospheric Research
* Copyright 2010-2015 Jeff McWhirter
*
* This library 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 library 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 library; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Main {
/**
* Write out a file to the filename specified.
*
* @param filename filename to write to
* @param contents file contents
*
* @throws FileNotFoundException if the file does not exist
* @throws IOException if there is a problem writing
*/
public static void writeFile(String filename, String contents) throws FileNotFoundException, IOException {
writeFile(new File(filename), contents);
}
/**
* Write out a file to the {@link File} specified.
*
* @param filename File to write to
* @param contents file contents
*
* @throws FileNotFoundException if the file does not exist
* @throws IOException if there is a problem writing
*/
public static void writeFile(File filename, String contents) throws FileNotFoundException, IOException {
writeBytes(filename, contents.getBytes());
}
/**
* Write out a file to the {@link File} specified.
*
* @param filename File to write to
* @param contents file contents
*
* @throws FileNotFoundException if the file does not exist
* @throws IOException if there is a problem writing
*/
public static void writeBytes(File filename, byte[] contents) throws FileNotFoundException, IOException {
FileOutputStream out = new FileOutputStream(filename);
out.write(contents);
out.flush();
out.close();
}
/**
* Write the bytes held by the iven string to the outputstream
*
* @param to stream to write to
* @param s string to write
*
* @throws Exception on badness
*/
public static void write(OutputStream to, String s) throws Exception {
to.write(s.getBytes());
}
/**
* _more_
*
* @param inputStream _more_
*/
public static void close(InputStream inputStream) {
if (inputStream == null) {
return;
}
try {
inputStream.close();
} catch (Exception ignore) {
}
}
/**
* _more_
*
* @param outputStream _more_
*/
public static void close(OutputStream outputStream) {
if (outputStream == null) {
return;
}
try {
outputStream.close();
} catch (Exception ignore) {
}
}
}
Related
- WriteFile(String filename, String content)
- writeFile(String fileName, String content)
- writeFile(String filename, String content)
- writeFile(String filename, String content)
- writeFile(String fileName, String content, String encoding)
- writeFile(String fileName, String contents)
- writeFile(String fileName, String contents)
- writeFile(String fileName, String contentStr, String charset)
- writeFile(String filename, String data)