Java Text File Save writeFile(String path, String fileName, String content)

Here you can find the source of writeFile(String path, String fileName, String content)

Description

Saves the String's content into the file.

License

Open Source License

Parameter

Parameter Description
path the path to the file.
fileName the filename.
content the content.

Declaration

public static void writeFile(String path, String fileName, String content) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class Main {
    /**//from www . ja v a2  s  .c o  m
     * Saves the String's content into the file.
     * 
     * @param path the path to the file.
     * @param fileName the filename.
     * @param content the content.
     */
    public static void writeFile(String path, String fileName, String content) {

        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())) {
                dirFile.mkdirs();
            }

            if (!(file.exists())) {
                file.createNewFile();
            }

            int count = 0;
            BufferedReader input = new BufferedReader(new FileReader(file));
            while ((s = input.readLine()) != null) {
                s1 += s + "\n";
                count++;
            }

            input.close();
            if (count <= 32) {
                s1 += content;
            }
            BufferedWriter output = new BufferedWriter(new FileWriter(file));
            output.write(s1);
            output.close();
            System.out.println("\nWritten to file: " + file.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. writeFile(String path, String content)
  2. writeFile(String path, String content)
  3. writefile(String path, String content, boolean append)
  4. writeFile(String path, String contents)
  5. writeFile(String path, String data)
  6. writeFile(String path, String fileName, String content, String charset)
  7. writeFile(String path, String fileName, String data)
  8. writeFile(String path, String text)
  9. writeFile(String path, String text)