FileIOToolKit neuste Version

Hier dann die neuste Version des FileIOToolKit .



import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

public class FileIOToolKit {

public FileIOToolKit() {

}

public static byte[] read(String filename) throws IOException {
return read(new File(filename));
}

public static byte[] read(File file) throws IOException {
byte[] in;
try (FileInputStream fis = new FileInputStream(file);) {
in = new byte[(int) file.length()];
fis.read(in);
}
catch (Exception e) {
throw e;
}
return in;
}

public static String readString(File file) throws IOException {
return readString(file, true);
}

public static String readString(File file, boolean reConstructEndOfLines) throws IOException {
StringBuilder buf = new StringBuilder();
try (BufferedReader in = new BufferedReader(new FileReader(file))) {
String str;
while ((str = in.readLine()) != null) {
if (reConstructEndOfLines) {
buf.append(str + "\n");
}
else {
buf.append(str);
}
}
}
catch (IOException e) {
throw e;
}
return buf.toString();
}

public static List<String> readStringAsList(File file) throws IOException {
List<String> buf = new ArrayList<String>();
try (BufferedReader in = new BufferedReader(new FileReader(file))) {
String str;
while ((str = in.readLine()) != null) {
buf.add(str);
}
}
catch (IOException e) {
throw e;
}
return buf;
}

public static String readString(String filename) throws IOException {
return readString(new File(filename));
}

public static String readString(String filename, boolean reConstructEndOfLines) throws IOException {
return readString(new File(filename), reConstructEndOfLines);
}

public static void write(File file, byte[] content) throws IOException {
try (RandomAccessFile rFile = new RandomAccessFile(file, "rw")) {
rFile.write(content);
}
catch (IOException e) {
throw e;
}
}

public static void write(String filename, byte[] content) throws IOException {
write(new File(filename), content);
}

public static void writeString(String filename, String content) throws IOException {
writeString(new File(filename), content, "UTF-8");
}

public static void writeStringOld(String filename, String content) throws IOException {
writeStringOld(new File(filename), content, "UTF-8");
}

public static void writeString(String filename, String content, String encoding) throws IOException {
writeString(new File(filename), content, encoding);
}

public static void writeString(File file, String content) throws IOException {
writeString(file, content, "UTF-8");
}

public static void writeString(File file, String content, String encoding) throws IOException {
try (RandomAccessFile rFile = new RandomAccessFile(file, "rw")) {
rFile.writeBytes(content);
}
catch (IOException e) {
throw e;
}
}

public static void writeStringOld(File file, String content, String encoding) throws IOException {
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding))) {
bw.write(content);
}
catch (IOException e) {
throw e;
}
}

public static void append(File file, byte[] content) throws IOException {
try (FileOutputStream fos = new FileOutputStream(file, true)) {
fos.write(content);

}
catch (IOException e) {
throw e;
}
}

public static void append(String filename, byte[] content) throws IOException {
append(new File(filename), content);
}

public static void appendString(File file, String content) throws IOException {
appendString(file, content, "UTF-8");
}

public static void appendString(File file, String content, String encoding) throws IOException {
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), encoding))) {
bw.write(content);
}
catch (IOException e) {
throw e;
}
}

public static void appendString(String filename, String content) throws IOException {
appendString(new File(filename), content);
}

public static void appendString(String filename, String content, String encoding) throws IOException {
appendString(new File(filename), content, encoding);
}

private static String convertByteArrayToHexString(byte[] arrayBytes) {
StringBuffer stringBuffer = new StringBuffer();
for (int ii = 0; ii < arrayBytes.length; ii++) {
stringBuffer.append(Integer.toString((arrayBytes[ii] & 0xff) + 0x100, 16).substring(1));
}
return stringBuffer.toString();
}

public static String createMD5HashOfFile(String filename) throws IOException, NoSuchAlgorithmException {
return createMD5HashOfFile(new File(filename));
}

public static String createMD5HashOfFile(File file) throws IOException, NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] content = read(file);
byte[] hash = digest.digest(content);
return convertByteArrayToHexString(hash);
}

public static String createMD5HashOfContent(byte[] content) throws IOException, NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] hash = digest.digest(content);
return convertByteArrayToHexString(hash);
}
}


Alles was man für die alltägliche Arbeit mit Dateien in Java braucht.
User annonyme 2015-12-28 21:07

write comment:
Four + = 9

Möchtest Du AdSense-Werbung erlauben und mir damit helfen die laufenden Kosten des Blogs tragen zu können?