0

I have a client that uploads a vcf file, and I get this file at server side and reads it contents and saves them to a txt file. But there is a character error when I try read it, if there is turkish characters it looks like "?". My read code is here:

            FileItemStream item = null;
            ServletFileUpload upload = new ServletFileUpload();
            FileItemIterator iterator = upload.getItemIterator(request);
            String encoding = null;
            while (iterator.hasNext()) {
                item = iterator.next();
                if ("fileUpload".equals(item.getFieldName())) {
                    InputStreamReader isr = new InputStreamReader(item.openStream(), "UTF-8");
                    String str = "";
                    String temp="";
                    BufferedReader br = new BufferedReader(isr);
                    while((temp=br.readLine()) != null){
                        str +=temp; 
                    }
                    br.close();
                    File f = new File("C:/sedat.txt");
                    BufferedWriter buf = new BufferedWriter(new FileWriter(f));
                    buf.write(str);
                    buf.close();
             }
gnat
  • 6,213
  • 108
  • 53
  • 73
Sedat Başar
  • 3,740
  • 3
  • 25
  • 28

1 Answers1

1
BufferedWriter buf = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "UTF-8"));

If this is production code, i would recommend writing the output straight to the file and not accumulating it in the string first. And, you could avoid any potential encoding issues by reading the source as an InputStream and writing as an OutputStream (and skipping the conversion to characters).

jtahlborn
  • 52,909
  • 5
  • 76
  • 118