We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Module for create files, append data to files, create folders, check if exists file or folders etc etc..
/* =====================================================================
Project : Simple operations with text/binary files.
Author : Erkosone
Created : 17 Jan 2017
Processing Version tested : 3.0.1
======================================================================== */
import java.nio.file.*;
import java.io.*;
import static java.nio.file.StandardOpenOption.*;
//----------------------------------------------------------------
void fwrite(String filename, byte[] data, boolean append){
Path file = Paths.get(filename);
OutputStream output = null;
try
{
if(append){
output = new BufferedOutputStream(Files.newOutputStream(file, APPEND));
}
else{
File f = new File(filename);
f.delete();
output = new BufferedOutputStream(Files.newOutputStream(file, CREATE));
}
output.write(data);
output.flush();
output.close();
}
catch(Exception e)
{
System.out.println("Message: " + e);
}
}
//----------------------------------------------------------------
void fwriteLine(String fileName, String newData, boolean appendData){
BufferedWriter bw = null;
try {
FileWriter fw = new FileWriter(fileName, appendData);
bw = new BufferedWriter(fw);
bw.write(newData + System.getProperty("line.separator"));
} catch (IOException e) {
} finally {
if (bw != null){
try {
bw.close();
} catch (IOException e) {}
}
}
}
//----------------------------------------------------------------
boolean fileExists(String filename) {
File file=new File(filename);
boolean exists = file.exists();
if (exists) {
return true;
}
else {
return false;
}
}
//----------------------------------------------------------------
void createFile(String filename){
BufferedWriter bw = null;
try {
FileWriter fw = new FileWriter(filename, true);
bw = new BufferedWriter(fw);
bw.write("");
} catch (IOException e) {
} finally {
if (bw != null){
try {
bw.close();
} catch (IOException e) {}
}
}
}
//----------------------------------------------------------------
void copyFile(String sourceFile, String destFile){
byte[] source = loadBytes(sourceFile);
saveBytes(destFile, source);
}
//----------------------------------------------------------------
boolean mkDir(String folderName){
File dir = new File(folderName);
dir.mkdir();
return fileExists(folderName);
}
Comments
are you serious? this is SO helpful!
thank you!
i'm not convinced
vs
there's no need to delete a file (lines 21, 22) if you want to write to it without appending - it's the default - https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#newOutputStream(java.nio.file.Path, java.nio.file.OpenOption...)
and your createFile() could be a one-liner that called your fwrite()
I'd love to see a simple function for writing and reading ancient INI-files.
like
etc.
Thanks a lot !!!
One note: this code comes from future (17 jan 2017) :)
thanks for sharing : )