title: Changing encoding in java
body: <p><br />
  I am writting a function that is should detect used charset and then switch it to utf-8. I am using juniversalchardet which is java port for universalchardet by mozilla.
  <br />This is my code:</p>
  
  <pre><code>private List&lt;List&lt;String&gt;&gt; setProperEncoding(List&lt;List&lt;String&gt;&gt; input) {
      try {
  
          // Detect used charset
          UniversalDetector detector = new UniversalDetector(null);
  
          int position = 0;
          while ((position &lt; input.size()) &amp; (!detector.isDone())) {
              String row = null;
              for (String cell : input.get(position)) {
                  row += cell;
              }
              byte[] bytes = row.getBytes();
              detector.handleData(bytes, 0, bytes.length);
              position++;
          }
          detector.dataEnd();
  
          Charset charset = Charset.forName(detector.getDetectedCharset());
          Charset utf8 = Charset.forName("UTF-8");
          System.out.println("Detected charset: " + charset);
  
          // rewrite input using proper charset
          List&lt;List&lt;String&gt;&gt; newLines = new ArrayList&lt;List&lt;String&gt;&gt;();
          for (List&lt;String&gt; row : input) {
              List&lt;String&gt; newRow = new ArrayList&lt;String&gt;();
              for (String cell : row) {
                  //newRow.add(new String(cell.getBytes(charset)));
                  ByteBuffer bb = ByteBuffer.wrap(cell.getBytes(charset));
                  CharBuffer cb = charset.decode(bb);
                  bb = utf8.encode(cb);
                  newRow.add(new String(bb.array()));
              }
              newLines.add(newRow);
          }
  
          return newLines;
  
      } catch (Exception e) {
          e.printStackTrace();
          return input;
      }
  }
  </code></pre>
  
  <p>My problem is that when I read file with chars of for example Polish alphabet, letters like ł,ą,ć and similiar are replaced by ? and other strange things. What am I doing wrong?</p>
  
  <p>EDIT:
  For compilation I am using eclipse.</p>
  
  <p>Method parameter is a result of reading MultipartFile. Just using FileInputStream to get every line and then splitting everyline by some separator (it is prepaired for xls, xlsx and csv files). Nothing special there.</p>