Description
Writes string to a file.
License
Apache License
Parameter
Parameter | Description |
---|
file | destination file |
s | source string |
bufferSize | buffer size |
encoding | java encoding string |
Exception
Parameter | Description |
---|
IOException | an exception |
Declaration
public static void writeString(File file, String s, int bufferSize, String encoding) throws IOException
Method Source Code
//package com.java2s;
//License from project: Apache License
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
/**/*from w w w .j ava 2 s .com*/
* Buffer size (32KB) for file string methods.
*/
public final static int STRING_BUFFER_SIZE = 32 * 1024;
/**
* Writes string to a file. Implicitly assumes that the file will be written the
* default encoding.
*
* @param fileName name of the destination file
* @param s source string
* @throws IOException
*/
public static void writeString(String fileName, String s) throws IOException {
writeString(new File(fileName), s, STRING_BUFFER_SIZE);
}
/**
* Writes string to a file. Implicitly assumes that the file will be written the
* default encoding.
*
* @param fileName name of the destination file
* @param s source string
* @param bufferSize buffer size
* @throws IOException
*/
public static void writeString(String fileName, String s, int bufferSize) throws IOException {
writeString(new File(fileName), s, bufferSize);
}
/**
* Writes string to a file. Implicitly assumes that the file will be written the
* default encoding.
*
* @param file destination file
* @param s source string
* @throws IOException
*/
public static void writeString(File file, String s) throws IOException {
writeString(file, s, STRING_BUFFER_SIZE);
}
/**
* Writes string to a file. Implicitly assumes that the file will be written the
* default encoding.
*
* @param file destination file
* @param s source string
* @param bufferSize buffer size
* @throws IOException
*/
public static void writeString(File file, String s, int bufferSize) throws IOException {
FileWriter fw = null;
BufferedWriter out = null;
if (s == null) {
return;
}
try {
fw = new FileWriter(file);
out = new BufferedWriter(fw, bufferSize);
out.write(s);
} finally {
if (out != null) {
out.close();
fw = null;
}
if (fw != null) {
fw.close();
}
}
}
/**
* Writes string to a file.
*
* @param fileName destination file name
* @param s source string
* @param encoding java encoding string
* @throws IOException
*/
public static void writeString(String fileName, String s, String encoding) throws IOException {
writeString(new File(fileName), s, STRING_BUFFER_SIZE, encoding);
}
/**
* Writes string to a file.
*
* @param fileName destination file name
* @param s source string
* @param bufferSize buffer size
* @param encoding java encoding string
* @throws IOException
*/
public static void writeString(String fileName, String s, int bufferSize, String encoding) throws IOException {
writeString(new File(fileName), s, bufferSize, encoding);
}
/**
* Writes string to a file.
*
* @param file destination file
* @param s source string
* @param encoding java encoding string
* @throws IOException
*/
public static void writeString(File file, String s, String encoding) throws IOException {
writeString(file, s, STRING_BUFFER_SIZE, encoding);
}
/**
* Writes string to a file.
*
* @param file destination file
* @param s source string
* @param bufferSize buffer size
* @param encoding java encoding string
* @throws IOException
*/
public static void writeString(File file, String s, int bufferSize, String encoding) throws IOException {
if (s == null) {
return;
}
FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter out = null;
try {
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos, encoding);
out = new BufferedWriter(osw, bufferSize);
out.write(s);
} finally {
if (out != null) {
out.close();
osw = null;
fos = null;
}
if (osw != null) {
osw.close();
fos = null;
}
if (fos != null) {
fos.close();
}
}
}
}
Related
- writeString(File file, String content, boolean append)
- writeString(File file, String data)
- writeString(File file, String str)
- writeString(File file, String str, String encoding)
- writeString(File file, String string)
- writeString(File file, String string)