I made a class that fetches text in a .TXT
file that is saved in UTF-8 format, and write to the Clipboard so I can paste it with CRTL + V into a field on another system. The problem is that when I use CRTL + V a ?
appears before the text. In some other places, if I use CRTL + V it glues right, but in the field I actually need to paste, 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 searched a lot and in notepad++
I found that if I paste something there and go to the "Code for ANSI" option it puts this ?
before the text, but I do not understand how to solve it in my% code%.
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++
}
}