java problem with UT8 format text file, please help?

2012-11-20 3:58 am
My java program converts a text file into a string, but when I save this text file with UT8 format, it adds a weird code to my string. How can I fix it? Is UT8 format incompatible with java or something?
Please clarify, thank you.


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import java.util.*;

/*
* This program reads a file and converts the content into a string. (String container)
* */

public class ReadFileString {


public static void main(String[] args) {

BufferedReader br = null;

try {

String cr;
String container = ""; //text in file would be converted into a string "container"

br = new BufferedReader(new FileReader("C:\\test.txt"));

while ((cr = br.readLine()) != null) {
container = container + cr + " ";

}

System.out.print(container);

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

}
}

回答 (2)

2012-11-20 4:08 am
✔ 最佳答案
Will you email me the file "test.txt" and I'll take a look at it.

My email address is: [email protected]

Also, how do you save the file as a UTF-8 text file? With your Java IDE after going to System.out?
2012-11-20 3:22 pm
Try add a UTF8 parameter for the bufferedreader


收錄日期: 2021-05-01 14:18:21
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20121119195835AAy4az7

檢視 Wayback Machine 備份