Here you can find the source of writeFile(String path, String fileName, String content)
Parameter | Description |
---|---|
path | the path to the file. |
fileName | the filename. |
content | the content. |
public static void writeFile(String path, String fileName, String content)
//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(); } } }