From: loial on 12 Jan 2010 04:25 I am reading and writing a files which contains the U.K pound sign £ But it is not being written correctly to the output file, even though I am specifying UTF-8 Should this code work? Reading : InputStream fr; BufferedReader br; try { fr = new FileInputStream(strDocumentFile); br = new BufferedReader(new InputStreamReader(fr, "UTF-8")); } catch (java.io.FileNotFoundException e) { String strReturn = "FileNotFoundException trying to open " + strInFile; traceError (strReturn); return strReturn; } catch (java.io.UnsupportedEncodingException e) { String strReturn = "FileNotFoundException trying to open " + strInFile; traceError (strReturn); return strReturn; } String s = null; do { try { s = br.readLine(); } catch (Exception e) { String strReturn = getExceptionStackString(e) + " while reading " + strInFile; traceError (strReturn); return strReturn; } Writing : OutputStream fw; BufferedWriter bw; try { fw = new FileOutputStream(strOutFile,true); bw = new BufferedWriter(new OutputStreamWriter(fw, "UTF-8")); } catch (java.io.FileNotFoundException e) { strReturn = "FileNotFoundException trying to open " + strOutFile; traceError (strReturn); return strReturn; } catch (java.io.UnsupportedEncodingException e) { strReturn = "FileNotFoundException trying to open " + strOutFile; traceError (strReturn); return strReturn; } bw.write(s);
From: Peter Duniho on 12 Jan 2010 05:30 loial wrote: > I am reading and writing a files which contains the U.K pound sign � > > But it is not being written correctly to the output file, even though > I am specifying UTF-8 > > Should this code work? I'm sure the code does what the Java documentation says it does. But, you haven't shown how you actually write out the � symbol � that is, where you define and use that character as part of the output � nor how you determine whether it's being read correctly, never mind have you provided a SSCCE to demonstrate whatever issue you're having. There's no way to know, given what code you did post, whether it would in fact work. Or, rather, taken literally the code you did post cannot possibly work, but there is some superset of that code that could. For your convenience, I've included a SSCCE that does in fact write a � symbol to an OutputStream, and then reads the � symbol back from an InputStream representing the same data. My example uses an in-memory stream, but you should be able to easily adapt it to handle a file instead. (If you copy-and-paste the code, make sure you put it in a source file that supports the � character, such as UTF-8�otherwise, you'll have to specify the character point explicitly using the \u notation). Pete import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; public class TestWriteUKPound { /** * @param args */ public static void main(String[] args) { ReadStream(new ByteArrayInputStream( ((ByteArrayOutputStream)WriteUKPound(new ByteArrayOutputStream())).toByteArray())); } private static OutputStream WriteUKPound(OutputStream stream) { OutputStreamWriter writer = null; try { writer = new OutputStreamWriter(stream, "UTF-8"); writer.write("�"); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (writer != null) { writer.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return stream; } private static void ReadStream(InputStream stream) { BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(stream, "UTF-8")); String str; while ((str = reader.readLine()) != null) { System.out.println(str); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
From: loial on 12 Jan 2010 06:07 Here is my one line test data file 001£999.99 Here is my test program code which I am compiling and running on linux. import java.io.*; import java.util.*; public class poundtesting { public static void main(String[] args) { String strReturn; String strDocumentFile = "/home/john/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 = "/home/john/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 } } }
From: RedGrittyBrick on 12 Jan 2010 06:46 loial wrote: > Here is my one line test data file > 001£999.99 > > Here is my test program code which I am compiling and running on > linux. > <snip> Your code works correctly on my PC. The output file contains a pound sign encoded as code-point 0xa3 which is correct for UTF-8 and for ISO-8859-1 Latin1. What byte-values do you see in your output file? Can you post a hex dump? -- RGB
From: John B. Matthews on 12 Jan 2010 06:50 In article <95a05c15-5f3a-4f97-8b25-8c0b884ce337(a)p8g2000yqb.googlegroups.com>, loial <jldunn2000(a)googlemail.com> wrote: > Here is my one line test data file > > 001£999.99 Your program appears to work as expected on Mac OS X 10.5.8, Java 1.5 and Ubuntu 9.10, Java version 1.6, as does Pete's. Here's the data file contents, identical before and after. It looks correctly encoded to me: 000000: 30 30 31 c2 a3 39 39 39 2e 39 39 0a 001£999.99. > Here is my test program code which I am compiling and running > on linux. What distribution, version and Java implementation? -- John B. Matthews trashgod at gmail dot com <http://sites.google.com/site/drjohnbmatthews>
|
Next
|
Last
Pages: 1 2 3 4 5 6 7 Prev: diff between openjdk, and sun/ibm javajdk Next: WHy jump in this code ? |