Skip to content

Instantly share code, notes, and snippets.

@heckenmann
Last active August 29, 2015 14:26
Show Gist options
  • Save heckenmann/36b5a0cb812366df2667 to your computer and use it in GitHub Desktop.
Save heckenmann/36b5a0cb812366df2667 to your computer and use it in GitHub Desktop.
Mit Java Dateien lesen und schreiben
package de.heckenmann.fileaccess;
/**
* http://heckenmann.de/dateien-schreiben-und-lesen/
**/
public class FileAccessTest {
public static final String FILENAME = "./test.txt";
/**
* TESTS.
* @param a args
*/
public static void main(final String... a) {
final FileWriterExample fwe = new FileWriterExample();
fwe.writeStringToFile(FILENAME, "Hallo Welt!");
fwe.writeByteToFile(FILENAME, "Hallo Welt!".getBytes());
final byte[] content = new FileReaderExample().readFileAsByte(FILENAME);
for (final byte b : content) {
System.out.print((char) b);
}
}
}
package de.heckenmann.fileaccess;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderExample {
/**
* Liest den Inhalt einer Datei und gibt ihn als byte[] zurück.
* !!! Dient nur zu Demozwecken. !!!
*
* @param filename Name / Pfad der Datei.
* @return Inhalt der Datei.
*/
public byte[] readFileAsByte(final String filename) {
final File f = new File(filename);
final byte[] result = new byte[(int) f.length()];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
//
// Ab hier geschieht die eigentliche Arbeit.
//
fis = new FileInputStream(f);
bis = new BufferedInputStream(fis);
bis.read(result); // Datei einlesen.
//
// Hier endet die eigentliche Arbeit.
//
} catch (final IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* Liest den Inhalt einer Datei und gibt ihn zurück.
* !!! Dient nur zu Demozwecken. !!!
*
* @param filename Name / Pfad der Datei.
* @return Inhalt der Datei.
*/
public String readFileAsString(final String filename) {
final File f = new File(filename);
final StringBuilder result = new StringBuilder();
FileReader fr = null;
BufferedReader br = null;
try {
//
// Ab hier geschieht die eigentliche Arbeit.
//
fr = new FileReader(f);
br = new BufferedReader(fr);
for (String toAppend = br.readLine(); toAppend != null; toAppend = br.readLine()) {
result.append(toAppend); // Jede Zeile wird eingelesen und zum Ergebnis hinzugefügt.
}
//
// Hier endet die eigentliche Arbeit.
//
} catch (final IOException e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
}
return result.toString();
}
}
package de.heckenmann.fileaccess;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterExample {
/**
* Schreibt den Inhalt vom Typ byte[] in eine Datei.
* @param filename Dateiname.
* @param content byte[]
*/
public void writeByteToFile(final String filename, final byte[] content) {
final File file = new File(filename);
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
// ####################################################
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(content);
bos.flush();
// ####################################################
} catch (final FileNotFoundException e) {
e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Schreibt den String in einer Datei.
* @param filename Dateiname
* @param content String
*/
public void writeStringToFile(final String filename, final String content) {
final File file = new File(filename);
FileWriter fw = null;
BufferedWriter bw = null;
try {
// ####################################################
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
bw.write(content);
bw.flush();
// ####################################################
} catch (final FileNotFoundException e) {
e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment