-2

Possible Duplicate:
Read special symbols from file

I am trying to copy a file to another file . The file is using encoding standard UTF8 and file also containing special characters . the program is not able to copy the special characters in another file in the same format the file is being disturbed with box shapes for special symbols.

 try 
  {
   BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(new File("path of the file")),"UTF8") ;
  BufferedWriter bw= new BufferedWriter(new OutputStreamReader(new FileOutputStream(new File("path of the output file");
    while(br.readLine()!=null)
   {
     //code here to read and write from a file to another.


    }

   }
  catch(Exception ex  
 { 
 ex.printStackTrace();
  }
Community
  • 1
  • 1
Adesh singh
  • 2,083
  • 9
  • 24
  • 36
  • 1
    Mmmm... did you try set encoding also and for **OutputStreamWriter**? – Andremoniy Dec 14 '12 at 10:52
  • there are many closing parenthesis missing here. To write a read line, you have to know what it is, you are loosing what `br.readLine()` returns. – jlordo Dec 14 '12 at 11:01

1 Answers1

0
BufferedWriter bw = new BufferedWriter(
        new OutputStreamWriter(
            new FileOutputStream(new File("path of the output file")),
            "UTF-8"));

Or in java 7 use Files.copy.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • given UTF8 in FileOutputStream constructor but still the same result – Adesh singh Dec 14 '12 at 10:56
  • In the OutputStreamWriter I suppose. Try using [JEdit](http://www.jedit.org/) or so, to inspect the original file. – Joop Eggen Dec 14 '12 at 11:10
  • @JoopEggen How to set encoding in Files.copy? – Half Blood Prince Apr 25 '16 at 09:36
  • 1
    @HalfBloodPrince as just a file must be copied as-is, there is no need for an encoding with Files.copy. Only if the file was read into java String variables and then written one needs to indicate the encoding of the files, as java keeps all text as Unicode internally. If you have a use-case where you need to convert the encoding: instead of Files.copy use the solution above, or use `Files.readAllLines/write` with charset. – Joop Eggen Apr 25 '16 at 10:45