title: Normalize Text - Read Each Character and remove spaces - Bad Enconding
body: <p>I am trying to have a program that normalizes my text, it removes multiple empty spaces, it prints the other characters from the original file, and also put spaces and start and ending symbols.</p>
  <p>So the conversion, after I write the txt file and open it, I see this content:</p>
  <blockquote>
  <p>numa situaã § ã £ o de emergãªncia mã © dica</p>
  </blockquote>
  <p>as you can see there are some weird characters that I don't want, maybe it's because of Encoding??
  This is a text in my language, Portuguese.</p>
  <p>Here is my code, how can I fix it?</p>
  <pre><code>public static void main(String[] args) throws IOException {
          
          Charset encoding = Charset.defaultCharset();
          
          InputStream in = new FileInputStream(new File(&quot;data.txt&quot;));
          Reader reader = new InputStreamReader(in, encoding);
          Reader buffer = new BufferedReader(reader);
          StringBuilder normalizedLanguage = new StringBuilder(&quot;&lt;&quot;);
          int r;
          while ((r = buffer.read()) != -1) {
              char ch = (char) r;
           
              
  
  
              boolean newline = false;
              boolean hasLetterBefore = false;
              boolean hasLetterAfter = false;
              char symbol = '-';
              int lines = 0;
  
              if (newline)
              {
                  normalizedLanguage.append(&quot;\n&lt;&quot;);
              }
  
  
              if (ch == '\r' || ch == '\n' )
              {
                  lines++;
                  normalizedLanguage.append(&quot;&gt;&quot;);
                  newline = true;
                  hasLetterBefore = false;
                  
  
              }
              else if (Character.isLetterOrDigit(ch))
              {
                  if (hasLetterBefore == true)
                  {
                      normalizedLanguage.append(Character.toString(symbol) + Character.toString(Character.toLowerCase(ch)));
                  }else{
                      normalizedLanguage.append(Character.toString(Character.toLowerCase(ch)));
                  }
  
                 
                  newline = false;
                  hasLetterBefore = true;
              }
              else if (ch == ' ')
              {
                  normalizedLanguage.append(Character.toString(ch));
                  newline = false;
                  hasLetterBefore = false;
              }
              else if (ch == '\t')
              {
                  System.out.println(&quot;Tab detected: &quot; + ch);
                  newline = false;
                  hasLetterBefore = false;
              }
              else
              {
                  //Símbolos, entre outros..
                  if (!hasLetterBefore)
                  {
                      normalizedLanguage.append(&quot; &quot; + Character.toString(ch) + &quot; &quot;);
                  }
                  else
                  {
                      symbol = ch;
                  }
                  newline = false;
                  
              }
  
  
          }
  
          String normalizedLanguageString = normalizedLanguage.toString().trim().replaceAll(&quot; +&quot;, &quot; &quot;);
          
          PrintWriter out = new PrintWriter(&quot;data_after.txt&quot;);
          
          out.println(normalizedLanguageString);
          out.close();
          
          buffer.close();
          reader.close();
          in.close();
  
      }
  </code></pre>
  <p>Thank you very much in advance ;)</p>