package com.cv.java.files;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
/**
* @author Chandra Vardhan
*
*/
public class FileWrite {
/**
* @param args
*/
public static void main(String[] args) {
FileWrite.writeContentToCreatedFile("c:/cv/test2.txt", null);
FileWrite.appendContentToExistingFile("c:/cv/test2.txt", null);
}
public static void writeContentToCreatedFile(final File file, List<String> content) {
File tempFile = file;
PrintWriter pw = null;
try {
tempFile = ensureDirFileAvailable(tempFile);
pw = new PrintWriter(tempFile);
if (content == null || !content.isEmpty()) {
content = provideDummyContent();
}
for (String value : content) {
pw.write(value + "\n");
}
pw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (pw != null)
pw.close();
}
}
public static void writeContentToCreatedFile(final String file, List<String> content) {
String tempFile = file;
PrintWriter pw = null;
try {
tempFile = ensureDirFileAvailable(tempFile);
pw = new PrintWriter(tempFile);
if (content == null || !content.isEmpty()) {
content = provideDummyContent();
}
for (String value : content) {
pw.write(value + "\n");
}
pw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (pw != null)
pw.close();
}
}
public static void appendContentToExistingFile(final File file, List<String> content) {
PrintWriter pw = null;
File tempFile = file;
try {
tempFile = ensureDirFileAvailable(tempFile);
pw = new PrintWriter(new FileOutputStream(tempFile, true));
if (content == null || !content.isEmpty()) {
content = provideDummyContent();
}
for (String value : content) {
pw.write(value + "\n");
}
pw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (pw != null)
pw.close();
}
}
public static void appendContentToExistingFile(final String file, List<String> content) {
String tempFile = file;
PrintWriter pw = null;
try {
tempFile = ensureDirFileAvailable(tempFile);
pw = new PrintWriter(new FileOutputStream(tempFile, true));
if (content == null || !content.isEmpty()) {
content = provideDummyContent();
}
for (String value : content) {
pw.write(value + "\n");
}
pw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (pw != null)
pw.close();
}
}
private static List<String> provideDummyContent() {
List<String> content = new ArrayList<String>();
for (int i = 1; i <= 5; ++i) {
content.add("" + i);
}
return content;
}
private static String ensureDirFileAvailable(final String fileAbsolutePath) {
String result = fileAbsolutePath;
try {
if (result == null) {
result = provideDummyFileStr();
}
if (result != null) {
File file = new File(result);
if (file.isFile() || !file.exists()) {
File parentFile = file.getParentFile();
if (parentFile != null && !parentFile.exists()) {
parentFile.mkdirs();
}
file.createNewFile();
}
}
} catch (Exception e) {
System.err.println("Problem while accessing the file path : : " + result);
e.printStackTrace();
}
return result;
}
private static File ensureDirFileAvailable(final File file) {
File result = file;
try {
if (result == null) {
result = provideDummyFile();
}
if (result != null) {
if (result.isFile() || !result.exists()) {
File parentFile = result.getParentFile();
if (parentFile != null && !parentFile.exists()) {
parentFile.mkdirs();
}
result.createNewFile();
}
}
} catch (Exception e) {
System.err.println("Problem while accessing the file path : : " + result);
e.printStackTrace();
}
return result;
}
private static String provideDummyFileStr() {
return "C:/cv/test.txt";
}
private static File provideDummyFile() {
return new File("C:/cv/test.txt");
}
} |
No comments:
Post a Comment