简体   繁体   中英

Java is changing my leading spaces to question marks

I am using this inside of a minecraft mod to read and write file and all the leading space are being converted to ? in the file.

file input sample:

{
   "ReturnToStart": "1b",

file out put sample:

{
???"ReturnToStart": "1b",
//xxxxxxxxxxxxxxxxxxxxxxx
var ips = new java.io.FileInputStream("ABC.json");
var fileReader = new java.io.InputStreamReader(ips,"UTF-8");
var data1 = fileReader.read();
var data;
var start1 = "";
while(data1 != -1) {    
data =  String.fromCharCode(data1);   
start1 = start1+data;
data1 = fileReader.read();
}
fileReader.close();
var  fileWriter = new java.io.FileWriter("J_out2.txt");
fileWriter.write(start1);
fileWriter.close();

 var ips = new java.io.FileReader("ABC.json"); var data1 = ips.read(); var data; var start1 = ""; while(data1 != -1) { data = String.fromCharCode(data1); if (data1 ==11 ||data1 ==12 || data1 ==10) { data1 = ips.read(); continue; } //npc.say(data1+" "+ data); data = String.fromCharCode(data1); start1 = start1+data; data1 = ips.read(); } ips.close(); npc.say(start1);

Well I took out line feed, vertical tab, and form feed (10,11,12) and it works.

It looks like you are using Nashorn in Java 8. Basically that is JavaScript running in a Java VM with access to all Java objects. I don't think those are normal spaces, and I suspect these are non breaking spaces (with code 160). It would be interesting to see what the value of data1 is at these positions.

The JavaScript method String.fromCharCode doesn't convert UTF-8 codes properly in Nashorn. Actually this will never work because in UTF-8 a single character can be split over multiple characters, and the value that comes back from read is limited to 16 bits. Not enough.

Below is probably what you need. I have included but commented the start1 variable because you may want to use that in your code, but is not needed.

var fileReader = new java.io.InputStreamReader(
    new java.io.FileInputStream("ABC.json"), "UTF-8");
var bufferedReader = new java.io.BufferedReader(fileReader);
var fileWriter = new java.io.OutputStreamWriter(
    new java.io.FileOutputStream("J_out2.txt"),"UTF-8");
var line;
// var start1=new java.lang.StringBuilder();
while(line=bufferedReader.readLine()) {
  // start1.append(line);
  // start1.append('\n');

  fileWriter.write(line);
  fileWriter.write('\n');
}
fileWriter.close();
bufferedReader.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM