Java, when I use CRTL+V, does the question mark appear in front of the text?

Asked

Viewed 79 times

0

I made a class that will fetch a text in a file .TXT which is saved in UTF-8 format, and record in Clipboard for me to paste with CRTL+V in a field of another system. The problem is that when I use the CRTL+V appears a ? before the text. In some other places, if I use the CRTL+V it glue straight, but in the field I need to paste really, it appears this ? before. Example, if the TXT contains "Hello World!" and I run the class and give CRTL+V it will paste "?Olá Mundo!". I did a lot of research and notepad++ found that if I paste something there and go on the option "Encode for ANSI" it puts this ? before the text, but I don’t understand how to solve it in my code JAVA.

Class:

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Metodos {


    public void paste(String texto) {
        Clipboard board = Toolkit.getDefaultToolkit().getSystemClipboard();
        ClipboardOwner selection = new StringSelection(texto);
        board.setContents((Transferable) selection, selection);

    }

    public String txt (String caminho){//"C:/Leia.txt"
   String linha = "";
      String ln = "";
    System.out.printf("\nConteúdo do arquivo texto:\n");
    try {
      File file = new File(caminho);
      BufferedReader in = new BufferedReader(
           new InputStreamReader(
                   new FileInputStream(file), "UTF8"));

      //String content = readFile(caminho, StandardCharsets.UTF_8);
        String str;

      while ((str = in.readLine()) != null){

        ln =str;

      }           
      in.close();
    } catch (IOException e) {
        System.err.printf("Erro na abertura do arquivo: %s.\n",
          e.getMessage());
    }
   return ln;

  }
    public static void main(String[] args) throws UnsupportedEncodingException {
        Metodos m = new Metodos();

        String a = m.txt("D:/pasta/1.txt");
        m.paste(a);
       //para testar usar CRTL+V no Notepad++ 
    }


}
  • And in which field do you need to paste this? I reproduced here and I had no problems, no appeared ?.

  • The field I need to paste is inside another system of my company, which I don’t know what language was developed. The problem is that in some fields ? appears and in others not. The only place q can be seen is in Notepas++. If you just put a string in the TXT file, and read it, and count how many Strings you have, it will appear 2, in function of having this ? in front. Then I solved the problem by deleting the first String from the text before writing to Clipboard.

1 answer

0

I decided to delete the first character as below:

String retornoDoMetodoPaste = retornoDoMetodoPaste.substring(1);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.