I want to copy the content of file \'A\' to file \'B\'. after the copying is done I want to clear the content of file \'A\' and want to write on it from its beginning. I can
Simple, write nothing!
FileOutputStream writer = new FileOutputStream("file.txt");
writer.write(("").getBytes());
writer.close();
FileOutputStream fos = openFileOutput("/*file name like --> one.txt*/", MODE_PRIVATE);
FileWriter fw = new FileWriter(fos.getFD());
fw.write("");
you can write a generic method as (its too late but below code will help you/others)
public static FileInputStream getFile(File fileImport) throws IOException {
FileInputStream fileStream = null;
try {
PrintWriter writer = new PrintWriter(fileImport);
writer.print(StringUtils.EMPTY);
fileStream = new FileInputStream(fileImport);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
writer.close();
}
return fileStream;
}
I don't believe you even have to write an empty string to the file.
PrintWriter pw = new PrintWriter("filepath.txt");
pw.close();
Just write:
FileOutputStream writer = new FileOutputStream("file.txt");
If you don't need to use the writer afterwards the shortest and cleanest way to do it would be like that:
new FileWriter("/path/to/your/file.txt").close();