Reading/writing file containg U.K. pound sign (£)
807580Jan 12 2010 — edited Jan 12 2010I am trying to read and write a text file that contains the U.K. pound sign, but the data is getting screwed in the output file, even though I am using UTF-8 in my InputStreamWriter and OutputStreamWriter. How do I get it to work?
Here is my one line test data file
001£999.99
The output file ends up looking like
001�999.99
Here is my test program code which I am compiling and running on Solaris 10
import java.io.*;
import java.util.*;
public class poundtesting {
public static void main(String[] args) {
String strReturn;
String strDocumentFile = "poundtest";
InputStream fr;
BufferedReader br = null;
try {
fr = new FileInputStream(strDocumentFile);
br = new BufferedReader(new InputStreamReader(fr, "UTF-8"));
}
catch (java.io.FileNotFoundException e) {
strReturn = "FileNotFoundException trying to open " +
strDocumentFile;
System.out.println(strReturn);
}
catch (java.io.UnsupportedEncodingException e) {
strReturn = "FileNotFoundException trying to open " +
strDocumentFile;
System.out.println(strReturn);
}
String s = null;
String outline = null;
do {
try {
s = br.readLine();
if (s != null) {
outline = s;
System.out.println(s);
}
} catch (Exception e) {
strReturn = " Error while reading " + strDocumentFile;
System.out.println(strReturn);
}
} while (s != null);
String strBatchFile = "poundout";
OutputStream fw;
BufferedWriter bw = null;
try {
fw = new FileOutputStream(strBatchFile,true);
bw = new BufferedWriter(new OutputStreamWriter(fw, "UTF-8"));
}
catch (java.io.FileNotFoundException e) {
strReturn = "FileNotFoundException trying to open " +
strBatchFile;
System.out.println(strReturn);
}
catch (java.io.UnsupportedEncodingException e) {
strReturn = "FileNotFoundException trying to open " +
strBatchFile;
System.out.println(strReturn);
}
try {
bw.write(outline);
bw.newLine();
} catch (IOException e) {
strReturn = "IOEXception trying to write " + strBatchFile ;
System.out.println(strReturn);
}
try {
br.close();
bw.close();
} catch (Exception e) {
// Don't care
}
}