Remove spec from string in Java

I have a line in a file containing a specification (from UTF-8). I want to convert this line to win-1251 and put it in a file.

I am trying to remove a specification from a string this way:

out.write(l.replace('\uFEFF','\0') + "\n"); 

But that will not work. Why?

The output of this line in the win-1251 file is:

 ?1,...SOME_TEXT_HERE 

First "?" The mark is illegal.

+8
source share
2 answers

You are replacing the specification U + 0000, not an empty string. You must replace the specification with an empty string, for example

 out.write(l.replace("\uFEFF", "") + "\n"); 
+14
source

@Jon Skeet's answer works very well.

We can remove the specification when converting from one encoding to another.

Here I converted from "UTF-8" to "SHIFT-JIS", the output of the SHIFT-JIS file without specification.

  File file =new File("input.txt"); InputStreamReader ireader = new InputStreamReader(new FileInputStream(file), "UTF-8"); BufferedReader br = new BufferedReader(ireader); File file2 =new File("removeBom.txt"); OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file2), "SHIFT-JIS"); PrintWriter pw = new PrintWriter(out); try { String line = br.readLine(); while(line!=null) { pw.println(line.replace("\uFEFF", "")); line=br.readLine(); } System.out.println("file copied!"); pw.close(); br.close(); } catch (IOException e) { System.out.println("error"+e); } 
0
source

Source: https://habr.com/ru/post/977988/


All Articles