You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
3.3 KiB
98 lines
3.3 KiB
package de.superx.elt.util; |
|
|
|
import java.io.BufferedReader; |
|
import java.io.BufferedWriter; |
|
import java.io.File; |
|
import java.io.FileInputStream; |
|
import java.io.FileNotFoundException; |
|
import java.io.FileOutputStream; |
|
import java.io.IOException; |
|
import java.io.InputStreamReader; |
|
import java.io.OutputStreamWriter; |
|
import java.io.StringWriter; |
|
import java.io.UnsupportedEncodingException; |
|
|
|
public class FileUtils { |
|
/* |
|
* Liest eine Datei mit vorgegebenem Encoding ein in einen String. |
|
* Wenn Datei nicht existiert, wird NULL zurückgegeben, |
|
* Wenn sie leer ist, kommt Leerstring zurück |
|
* @author Quathamer |
|
* @param filePath |
|
* @param encoding (Default ist utf-8) |
|
*/ |
|
|
|
public static String getFileContentsWithEncoding(String filePath, String encoding) { |
|
File f = new File(filePath); |
|
if (!f.exists()) { |
|
System.out.println("Fehler: Datei " + filePath + " existiert nicht."); |
|
return null; |
|
} |
|
String fileContents = ""; |
|
if (encoding == null || encoding.trim().equals("")) { |
|
encoding = System.getProperty("file.encoding"); |
|
} |
|
try { |
|
// --- IputStream und OutputStream generieren ---// |
|
FileInputStream fis = new FileInputStream(f); |
|
// Wenn Quelldatei Unicode, dann speziellen Reader nutzen |
|
BufferedReader in; |
|
//BufferedReader ist schneller bei großen Dateien |
|
in = new BufferedReader(new InputStreamReader(fis, encoding)); |
|
// --- Output-Stream der temporären Datei erzeugen ---// |
|
StringWriter out = new StringWriter(); |
|
// --- Verarbeiten der Datei ---// |
|
String text; |
|
text = in.readLine(); |
|
while (text != null) { // Datei nicht leer |
|
out.write(text); |
|
out.write(System.getProperty("line.separator")); |
|
text = in.readLine(); |
|
} |
|
if (!(out == null)) { |
|
fileContents = out.toString(); |
|
} |
|
} catch (FileNotFoundException e) { |
|
// TODO Auto-generated catch block |
|
e.printStackTrace(); |
|
} catch (UnsupportedEncodingException e) { |
|
// TODO Auto-generated catch block |
|
e.printStackTrace(); |
|
} catch (IOException e) { |
|
// TODO Auto-generated catch block |
|
e.printStackTrace(); |
|
} |
|
return fileContents; |
|
} |
|
|
|
public static void saveFileContentsWithEncoding(String filename, String contents, String encoding) throws |
|
|
|
FileNotFoundException, |
|
IOException |
|
{ |
|
|
|
|
|
File f = new File(filename); |
|
BufferedReader in; |
|
BufferedWriter out; |
|
|
|
//Default encoding ist utf-8 |
|
if (encoding == null) encoding = System.getProperty("file.encoding"); |
|
// --- Output-Stream der temporären Datei erzeugen ---// |
|
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), encoding)); |
|
|
|
out.write(contents); |
|
|
|
out.close(); |
|
|
|
|
|
}//Ende der Methode |
|
public static String convertToIso(String inp, String dbsystem) throws java.io.UnsupportedEncodingException |
|
{ |
|
String outp = new String(inp.getBytes("ISO-8859-1")); |
|
if (dbsystem.equalsIgnoreCase("postgres")) { |
|
outp = outp.replaceAll("´", " "); |
|
} |
|
return outp; |
|
} |
|
}
|
|
|