FileWriter (Java)를 사용하여 UTF-8로 파일을 작성 하시겠습니까?
그러나 다음 코드가 있지만 외국 문자를 처리하기 위해 UTF-8 파일로 작성하고 싶습니다. 이 작업을 수행하는 방법이 있습니까? 매개 변수가 필요합니까?
도움을 주시면 정말 감사하겠습니다. 감사.
try {
BufferedReader reader = new BufferedReader(new FileReader("C:/Users/Jess/My Documents/actresses.list"));
writer = new BufferedWriter(new FileWriter("C:/Users/Jess/My Documents/actressesFormatted.csv"));
while( (line = reader.readLine()) != null) {
//If the line starts with a tab then we just want to add a movie
//using the current actor's name.
if(line.length() == 0)
continue;
else if(line.charAt(0) == '\t') {
readMovieLine2(0, line, surname.toString(), forename.toString());
} //Else we've reached a new actor
else {
readActorName(line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
안전한 인코딩 생성자
Java가 인코딩 오류를 올바르게 알리도록하는 것은 까다 롭습니다. 당신은 사용해야 가장 자세한 그리고, 슬프게도, 적어도 사용되는 각각의 네 개의 다른 contructors의를 InputStreamReader
하고 OutputStreamWriter
인코딩 결함에 적절한 예외를받을 수 있습니다.
파일 I / O의 경우 항상 두 번째 인수 OutputStreamWriter
와 InputStreamReader
멋진 인코더 인수에 대해 항상 사용하십시오 .
Charset.forName("UTF-8").newEncoder()
다른 더 멋진 가능성도 있지만 예외 처리에는 세 가지 간단한 가능성 중 어느 것도 작동하지 않습니다. 다음을 수행합니다.
OutputStreamWriter char_output = new OutputStreamWriter(
new FileOutputStream("some_output.utf8"),
Charset.forName("UTF-8").newEncoder()
);
InputStreamReader char_input = new InputStreamReader(
new FileInputStream("some_input.utf8"),
Charset.forName("UTF-8").newDecoder()
);
달리기에 관해서
$ java -Dfile.encoding=utf8 SomeTrulyRemarkablyLongcLassNameGoeShere
문제는 문자 스트림에 대해 전체 인코더 인수 형식을 사용하지 않으므로 인코딩 문제를 다시 놓칠 수 있다는 것입니다.
더 긴 예
다음은 파일 대신 프로세스를 관리하는 더 긴 예제입니다. 여기서는 두 개의 서로 다른 입력 바이트 스트림과 하나의 출력 바이트 스트림을 모두 전체 예외 처리 를 통해 UTF-8 문자 스트림 으로 승격합니다 .
// this runs a perl script with UTF-8 STD{IN,OUT,ERR} streams
Process
slave_process = Runtime.getRuntime().exec("perl -CS script args");
// fetch his stdin byte stream...
OutputStream
__bytes_into_his_stdin = slave_process.getOutputStream();
// and make a character stream with exceptions on encoding errors
OutputStreamWriter
chars_into_his_stdin = new OutputStreamWriter(
__bytes_into_his_stdin,
/* DO NOT OMIT! */ Charset.forName("UTF-8").newEncoder()
);
// fetch his stdout byte stream...
InputStream
__bytes_from_his_stdout = slave_process.getInputStream();
// and make a character stream with exceptions on encoding errors
InputStreamReader
chars_from_his_stdout = new InputStreamReader(
__bytes_from_his_stdout,
/* DO NOT OMIT! */ Charset.forName("UTF-8").newDecoder()
);
// fetch his stderr byte stream...
InputStream
__bytes_from_his_stderr = slave_process.getErrorStream();
// and make a character stream with exceptions on encoding errors
InputStreamReader
chars_from_his_stderr = new InputStreamReader(
__bytes_from_his_stderr,
/* DO NOT OMIT! */ Charset.forName("UTF-8").newDecoder()
);
지금 당신은 오류를 인코딩하는 모든 인상 예외가 각각라는 것을 세 가지 문자 스트림을 chars_into_his_stdin
, chars_from_his_stdout
하고 chars_from_his_stderr
.
This is only slightly more complicated that what you need for your problem, whose solution I gave in the first half of this answer. The key point is this is the only way to detect encoding errors.
Just don’t get me started about PrintStream
s eating exceptions.
Ditch FileWriter
and FileReader
, which are useless exactly because they do not allow you to specify the encoding. Instead, use
new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)
and
new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
You need to use the OutputStreamWriter
class as the writer parameter for your BufferedWriter
. It does accept an encoding. Review javadocs for it.
Somewhat like this:
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("jedis.txt"), "UTF-8"
));
Or you can set the current system encoding with the system property file.encoding
to UTF-8.
java -Dfile.encoding=UTF-8 com.jediacademy.Runner arg1 arg2 ...
You may also set it as a system property at runtime with System.setProperty(...)
if you only need it for this specific file, but in a case like this I think I would prefer the OutputStreamWriter
.
By setting the system property you can use FileWriter
and expect that it will use UTF-8 as the default encoding for your files. In this case for all the files that you read and write.
EDIT
Starting from API 19, you can replace the String "UTF-8" with
StandardCharsets.UTF_8
As suggested in the comments below by tchrist, if you intend to detect encoding errors in your file you would be forced to use the
OutputStreamWriter
approach and use the constructor that receives a charset encoder.Somewhat like
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder(); encoder.onMalformedInput(CodingErrorAction.REPORT); encoder.onUnmappableCharacter(CodingErrorAction.REPORT); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("jedis.txt"),encoder));
You may choose between actions
IGNORE | REPLACE | REPORT
Also, this question was already answered here.
With Chinese text, I tried to use the Charset UTF-16 and lucklily it work.
Hope this could help!
PrintWriter out = new PrintWriter( file, "UTF-16" );
Since Java 7 there is an easy way to handle character encoding of BufferedWriter and BufferedReaders. You can create a BufferedWriter directly by using the Files class instead of creating various instances of Writer. You can simply create a BufferedWriter, which considers character encoding, by calling:
Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8);
You can find more about it in JavaDoc:
Since Java 11 you can do:
FileWriter fw = new FileWriter("filename.txt", Charset.forName("utf-8"));
In my opinion
If you wanna write follow kind UTF-8.You should create a byte array.Then,you can do such as the following: byte[] by=("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+"Your string".getBytes();
Then, you can write each byte into file you created. Example:
OutputStream f=new FileOutputStream(xmlfile);
byte[] by=("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+"Your string".getBytes();
for (int i=0;i<by.length;i++){
byte b=by[i];
f.write(b);
}
f.close();
참고URL : https://stackoverflow.com/questions/9852978/write-a-file-in-utf-8-using-filewriter-java
'programing tip' 카테고리의 다른 글
WGET 시간이 초과됩니까? (0) | 2020.10.26 |
---|---|
WPF : 대화 / 프롬프트 만들기 (0) | 2020.10.26 |
Mongoose를 사용하여 컬렉션에서 모든 문서를 제거하려면 어떻게해야합니까? (0) | 2020.10.26 |
Windows 10 SSH 키 (0) | 2020.10.26 |
TypeScript에서 '확장'과 '구현'의 차이점은 무엇입니까? (0) | 2020.10.26 |