encoding einer text-datei

lumo

Top Contributor
Hallo,

ich weiss, dass das schon etliche male durchgekaut wurde.
zb hier: XML und Co. > Encoding Problem
und hier: XML und Co. > Umlaute in XStream

bei mir gehts zwar auch um xml, allerdings brauch ich das thema encoding auch noch in anderen files, die ich speichern muss, darum im allgemeinen forum 😉

ich habe mir aus den diversen threads (und google artikeln) ein kleines testprogramm zusammengestellt, das meine datei erstellt (incl encoding) und dann wieder ausliest.

also im genauen erstelle ich eine datei mit xml header (könnte es auch plain-text reinschreiben...)
in den header schreibe ich mir das encoding rein, mit dem es gespeichert wurde (oder gespeichert werden sollte 😉)

danach habe ich zwei funktionen.
eine die versucht das file zu laden mit dem encoding das im header steht
und eine die das file lädt mit dem encoding das ihr der fileinputstream gibt.

und überraschung! es kommt jedesmal bockmist raus!
wo liegt mein fehler?

hier mein testprogramm:

Java:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;

public class XML {

	public static void save() {
		try {
			String path = "sample.xml";
			String encoding = "UTF-8"; // "UTF-8", "ISO-8859-1"
			String lineFeed = "\n";
			String content = String.format(
					"<?xml version=\"1.0\" encoding=\"%s\"?>", encoding);
			String sampledata = "<sample>öäüÖÄÜß@€²³|µ~</sample>";

			FileOutputStream fos = new FileOutputStream(path);
			PrintWriter writer = new PrintWriter(new OutputStreamWriter(fos,
					encoding), true);
			writer.append(content);
			writer.append(lineFeed);
			writer.append(sampledata);
			writer.close();
			fos.close();
			// if you use xstream call it this way:
			// xstream.toXML(JTextField, writer);

		} catch (Exception e) {
			System.out.println("Error");
		}
	}

	public static void loadWithInputStreamReaderEncoding() {
		System.out.println("loadWithInputStreamReaderEncoding()");
		try {
			String path = "sample.xml";
			FileInputStream fis = new FileInputStream(path);
			BufferedInputStream bin = new BufferedInputStream(fis);
			InputStreamReader in = new InputStreamReader(bin);// , "8859_1"
			BufferedReader br = new BufferedReader(in);

			System.out
					.println("the file is encoded using: " + in.getEncoding());
			String theLine;
			// trying to output the data in the encodig of the file
			System.setOut(new PrintStream(new FileOutputStream(
					FileDescriptor.out), true, in.getEncoding()));
			System.out.println("trying encoding: "+in.getEncoding());
			while ((theLine = br.readLine()) != null) {
				System.out.println(theLine);
			}
			// close all
			br.close();
			in.close();
			bin.close();
			fis.close();
		} catch (Exception e) {
			System.out.println("Error");
			e.printStackTrace();
		}
	}
	public static void loadWithXMLencoding() {
		System.out.println("loadWithXMLencoding()");
		try {
			String path = "sample.xml";
			FileInputStream fis = new FileInputStream(path);
			BufferedInputStream bin = new BufferedInputStream(fis);
			InputStreamReader in = new InputStreamReader(bin);// , "8859_1"
			BufferedReader br = new BufferedReader(in);
			
			System.out
			.println("the file is encoded using: " + in.getEncoding());
			String xmlHeader = br.readLine();
			String encoding = xmlHeader.substring(xmlHeader
					.lastIndexOf("encoding=\"")
					+ "encoding=\"".length(), xmlHeader.lastIndexOf("\""));
			
			String theLine;
			// trying to output the data in the encodig of the header
			System.setOut(new PrintStream(new FileOutputStream(
					FileDescriptor.out), true, encoding));
			System.out.println("trying encoding: "+encoding);
			System.out.println(xmlHeader);
			while ((theLine = br.readLine()) != null) {
				System.out.println(theLine);
			}
			br.close();
			
			// close all
			br.close();
			in.close();
			bin.close();
			fis.close();
		} catch (Exception e) {
			System.out.println("Error");
			e.printStackTrace();
		}
	}
	
	public static void load() {
		loadWithInputStreamReaderEncoding();
		loadWithXMLencoding();
	}

	public static void main(String[] args) {
		XML.save();
		XML.load();
	}
}

EDIT: wenn ich statt dem writer das fos verwende, also
Java:
			fos.write(content.getBytes());
			fos.write(lineFeed.getBytes());
			fos.write(sampledata.getBytes());
kommt zumindest beim file encoding das korrekte raus. aber nicht beim utf-8!?
 
Zuletzt bearbeitet von einem Moderator:
In welchem Encoding liegen denn deine Sourcen vor?
Unter Windows ist das Regelmässig nicht UTF-8... soll heissen:
Deine Sonderzeichen im Quelltext sind schon "müll" bevor sie in die XML Datei kommen 😉
 
harr!

dann muss ich wohl das file-encoding der dateien schon auf utf-8 umstellen 🙂

danke für den hinweis!
(nach dem umstellen der file auf utf-8 hats geklappt 😳:lol🙂
 

Zurück
Oben