How to append Same File name With Date Append in Java?
Please Find Code Below :
==================Append.java=======================
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
/**
*
* @author ananddw
*/
public class Append {
public static void main(String[] args) throws IOException {
/*
Date date = new Date();
File file1 = new File("/home/ananddw/Anand.json");
String file2 = file1.getParent().toString();
System.out.println("file 2 call ===>" + file2);
boolean b = false;
if (!(file1.exists())) {
b = file1.createNewFile();
}
if (b) {
System.out.println("Empty File successfully created");
} else {
String fileContent = "";
String filename = file1.toString() + date;
try {
File f = new File(file1.toString());
FileInputStream inp = new FileInputStream(f);
byte[] bf = new byte[(int) f.length()];
inp.read(bf);
fileContent = new String(bf, "UTF-8");
System.out.println("file===>" + fileContent);
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
System.out.println("file name in else===>" + filename);
FileWriter fw = new FileWriter(filename, true);
System.out.println("FileWriter===>"+fw);
String see = null;
fw.write("Test");
fw.close();
System.out.println("Failed to create File");
}
}
}
==================Append.java=======================
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
/**
*
* @author ananddw
*/
public class Append {
public static void main(String[] args) throws IOException {
/*
Date date = new Date();
File file1 = new File("/home/ananddw/Anand.json");
String file2 = file1.getParent().toString();
System.out.println("file 2 call ===>" + file2);
boolean b = false;
if (!(file1.exists())) {
b = file1.createNewFile();
}
if (b) {
System.out.println("Empty File successfully created");
} else {
String fileContent = "";
String filename = file1.toString() + date;
try {
File f = new File(file1.toString());
FileInputStream inp = new FileInputStream(f);
byte[] bf = new byte[(int) f.length()];
inp.read(bf);
fileContent = new String(bf, "UTF-8");
System.out.println("file===>" + fileContent);
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
System.out.println("file name in else===>" + filename);
FileWriter fw = new FileWriter(filename, true);
System.out.println("FileWriter===>"+fw);
String see = null;
fw.write("Test");
fw.close();
System.out.println("Failed to create File");
}
}
}
Comments
Post a Comment