Imo John
3 years ago
commit
ed7f70012a
7 changed files with 174 additions and 0 deletions
@ -0,0 +1,12 @@
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<classpath> |
||||
<classpathentry kind="src" path="src"/> |
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"> |
||||
<attributes> |
||||
<attribute name="module" value="true"/> |
||||
</attributes> |
||||
</classpathentry> |
||||
<classpathentry kind="lib" path="lib/juniversalchardet-2.5.0-SNAPSHOT.jar"/> |
||||
<classpathentry kind="lib" path="lib/superx5.0.jar"/> |
||||
<classpathentry kind="output" path="bin"/> |
||||
</classpath> |
@ -0,0 +1,17 @@
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<projectDescription> |
||||
<name>FileConverting</name> |
||||
<comment></comment> |
||||
<projects> |
||||
</projects> |
||||
<buildSpec> |
||||
<buildCommand> |
||||
<name>org.eclipse.jdt.core.javabuilder</name> |
||||
<arguments> |
||||
</arguments> |
||||
</buildCommand> |
||||
</buildSpec> |
||||
<natures> |
||||
<nature>org.eclipse.jdt.core.javanature</nature> |
||||
</natures> |
||||
</projectDescription> |
Binary file not shown.
Binary file not shown.
@ -0,0 +1,39 @@
@@ -0,0 +1,39 @@
|
||||
package de.superx.fileConverting; |
||||
|
||||
import org.mozilla.universalchardet.UniversalDetector; |
||||
|
||||
public class EncodingDetector { |
||||
|
||||
public static void main(String[] args) |
||||
|
||||
|
||||
throws java.io.IOException { |
||||
byte[] buf = new byte[4096]; |
||||
String fileName = args[0]; |
||||
java.io.FileInputStream fis = new java.io.FileInputStream(fileName); |
||||
|
||||
// (1)
|
||||
UniversalDetector detector = new UniversalDetector(null); |
||||
|
||||
// (2)
|
||||
int nread; |
||||
while ((nread = fis.read(buf)) > 0 && !detector.isDone()) { |
||||
detector.handleData(buf, 0, nread); |
||||
} |
||||
|
||||
// (3)
|
||||
detector.dataEnd(); |
||||
|
||||
// (4)
|
||||
String encoding = detector.getDetectedCharset(); |
||||
if (encoding != null) { |
||||
System.out.println("Detected encoding = " + encoding); |
||||
} else { |
||||
System.out.println("No encoding detected."); |
||||
} |
||||
|
||||
// (5)
|
||||
detector.reset(); |
||||
|
||||
} |
||||
} |
@ -0,0 +1,105 @@
@@ -0,0 +1,105 @@
|
||||
package de.superx.fileConverting; |
||||
|
||||
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.FileReader; |
||||
import java.io.IOException; |
||||
import java.io.InputStreamReader; |
||||
import java.io.OutputStreamWriter; |
||||
import java.io.Reader; |
||||
import java.io.StringWriter; |
||||
import java.io.UnsupportedEncodingException; |
||||
|
||||
public class Utf8Converter { |
||||
|
||||
public static void main(String[] args) { |
||||
// TODO Auto-generated method stub
|
||||
|
||||
|
||||
String quellDatei=args[0]; |
||||
String encoding="ISO-8859-1"; |
||||
String text; |
||||
|
||||
|
||||
|
||||
text=getFileContentsWithEncoding(quellDatei,encoding); |
||||
try { |
||||
saveFileContentsWithEncoding(quellDatei+"_utf8.csv",text,"UTF-8"); |
||||
} catch (Exception e) { |
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace(); |
||||
} |
||||
System.out.println(text); |
||||
|
||||
} |
||||
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
|
||||
|
||||
} |
Loading…
Reference in new issue