Description
Writes to the file with the given name
License
Open Source License
Parameter
Parameter | Description |
---|
name | the file name |
s | the contents of the file |
Exception
Parameter | Description |
---|
IOException | if an I/O problem occurs |
Declaration
public final static void writeFile(final String name, final String s) throws IOException
Method Source Code
//package com.java2s;
/**/*w w w . j ava 2 s. co m*/
* The contents of this file are subject to the Regenstrief Public License
* Version 1.0 (the "License"); you may not use this file except in compliance with the License.
* Please contact Regenstrief Institute if you would like to obtain a copy of the license.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) Regenstrief Institute. All Rights Reserved.
*/
import java.io.*;
public class Main {
private final static boolean DEF_APPEND = false;
/**
* Writes to the file with the given name
*
* @param name the file name
* @param s the contents of the file
* @throws IOException if an I/O problem occurs
**/
public final static void writeFile(final String name, final String s) throws IOException {
writeFile(name, s, false);
}
/**
* Writes to the file with the given name
*
* @param name the file name
* @param s the contents of the file
* @param append whether data should be appended to the end of the file instead of overwriting
* it
* @throws IOException if an I/O problem occurs
**/
public final static void writeFile(final String name, final String s, final boolean append) throws IOException {
//writer = new BufferedWriter(new FileWriter(name, append));
final BufferedWriter writer = new BufferedWriter(getFileWriter(name, append));
writer.write(s);
writer.close();
}
public final static Writer getFileWriter(final String absolutePath) throws IOException {
return getFileWriter(absolutePath, DEF_APPEND);
}
public final static Writer getFileWriter(final String absolutePath, final boolean append) throws IOException {
return new OutputStreamWriter(getFileOutputStream(absolutePath, append));
}
public final static Writer getFileWriter(final File f) throws IOException {
return getFileWriter(f, DEF_APPEND);
}
public final static Writer getFileWriter(final File f, final boolean append) throws IOException {
return new OutputStreamWriter(getFileOutputStream(f, append));
}
/**
* Retrieves a OutputStream for the given absolute path, creating any necessary directories
*
* @param absolutePath the absolute path
* @return the OutputStream
* @throws IOException if an I/O problem occurs
**/
public final static OutputStream getFileOutputStream(final String absolutePath) throws IOException {
return getFileOutputStream(absolutePath, DEF_APPEND);
}
/**
* Retrieves a OutputStream for the given absolute path, creating any necessary directories
*
* @param absolutePath the absolute path
* @param append whether the file should be opened in append mode
* @return the OutputStream
* @throws IOException if an I/O problem occurs
**/
public final static OutputStream getFileOutputStream(final String absolutePath, final boolean append)
throws IOException {
return getFileOutputStream(new File(absolutePath), append);
}
public final static OutputStream getFileOutputStream(final File f) throws IOException {
return getFileOutputStream(f, DEF_APPEND);
}
public final static OutputStream getFileOutputStream(final File f, final boolean append) throws IOException {
final File dir = f.getParentFile();
if ((dir != null) && !dir.exists()) {
dir.mkdirs();
}
return new FileOutputStream(f, append);
}
/**
* Retrieves whether the file with the given name exists
*
* @param fileName the file name
* @return whether the file exists
**/
public final static boolean exists(final String fileName) {
return fileName == null ? false : new File(fileName).exists();
}
}
Related
- writeFile(final File destination, final List contents)
- writeFile(final File f, final String content)
- writeFile(final File outputFile, final String data, final String encoding)
- writeFile(final String aText, final File aFile, final String aEncoding)
- writeFile(final String file, final String text)
- writeFile(final String nameFile, final List listMsg)
- writeFile(final String outputDir, final String response, final String fileName)
- writeFile(IFile annotationFile, StringBuffer head, String annotatedSignature, String nextLines, BufferedReader tailReader, IProgressMonitor monitor)
- writeFile(IFile file, final InputStream contents, final String charset, IProgressMonitor monitor)