Java Utililty Methods Text File Save

List of utility methods to do Text File Save

Description

The list of methods to do Text File Save are organized into topic(s).

Method

booleanwriteFile(String filePath, List lines)
Writes the given lines to the file at the given path.
File file = new File(filePath);
if (!file.exists()) {
    try {
        file.createNewFile();
    } catch (Exception failure) {
        return false;
try (PrintWriter writer = new PrintWriter(file.getAbsolutePath(), "UTF-8")) {
    for (String line : lines) {
        writer.println(line);
    writer.flush();
    writer.close();
} catch (Exception failure) {
    return false;
return true;
voidwritefile(String path, String content)
writefile
FileWriter out = new FileWriter(path);
out.write(content);
out.close();
voidwritefile(String path, String content)
writefile
File f = new File(path);
if (f.exists())
    f.delete();
FileWriter out = new FileWriter(f);
out.write(content);
out.close();
booleanwriteFile(String path, String content)
write File
boolean successed = false;
OutputStream fos = null;
Writer osw = null;
try {
    File file = new File(path);
    if (!file.exists()) {
        file.getParentFile().mkdirs();
        file.createNewFile();
...
StringwriteFile(String path, String content)
write File
byte[] matter = content.getBytes("GBK");
return writeFile(path, matter, false);
voidwritefile(String path, String content, boolean append)
writefile
BufferedWriter bw;
File targetFile;
try {
    targetFile = new File(path);
    if (targetFile.exists() == false) {
        targetFile.createNewFile();
    FileWriter fw = new FileWriter(targetFile, append);
...
voidwriteFile(String path, String contents)
write File
PrintWriter out = null;
try {
    out = new PrintWriter(new BufferedWriter(new FileWriter(path)));
    out.print(contents);
    out.flush();
} finally {
    if (out != null) {
        out.close();
...
voidwriteFile(String path, String data)
write File
File file = new File(path.substring(0, path.lastIndexOf("/")));
file.mkdirs();
FileWriter fileWriter = new FileWriter(path);
BufferedWriter out_j = new BufferedWriter(fileWriter);
out_j.write(data);
out_j.close();
voidwriteFile(String path, String fileName, String content)
Saves the String's content into the file.
String s = new String();
String s1 = new String();
String fullPath = path + File.separator + fileName;
String dirPath = path;
try {
    File file = new File(fullPath);
    File dirFile = new File(dirPath);
    if (!(dirFile.isDirectory())) {
...
StringwriteFile(String path, String fileName, String content, String charset)
write File
mkDir(path);
String filePath = path + File.separator + fileName;
File file = new File(filePath);
if (file.exists()) {
    file.delete();
file.createNewFile();
FileOutputStream fops = new FileOutputStream(file);
...