如何读取一个文件,用某种特定的编码写入另一个文件(JAVA)

如何读取一个文件,用某种特定的编码写入另一个文件(JAVA),第1张

import java.io.File

import java.io.IOException

public class CovertEncode {

public static void main(String args[]) throws Exception {

CovertFile("d:\\1.txt")

}

public static void CovertFile(String filename) throws IOException {

String backupfilename = filename + ".backup"

File f = new File(filename)

File outf = new File(backupfilename)

f.renameTo(outf)

java.io.FileInputStream in = new java.io.FileInputStream(backupfilename)

java.io.InputStreamReader isr = new java.io.InputStreamReader(in, "Big5")

java.io.BufferedReader br = (new java.io.BufferedReader(isr))

// open output stream

java.io.FileOutputStream out = new java.io.FileOutputStream(filename)

java.io.BufferedWriter bw = new java.io.BufferedWriter(

new java.io.OutputStreamWriter(out, "GBK"))

char[] buffer = new char[4096]

int len

while ((len = br.read(buffer)) != -1)

bw.write(buffer, 0, len)

br.close()

bw.flush()

bw.close()

outf.deleteOnExit()

}

}

过去的所谓"unicode编码"其实是指ucs-16编码,unicode是指字符集,连gb18030都是unicode编码。而“unicode

big

endian

”是如笑utf-16be编码,也属于unicode编码.

可以改成

outputstreamwriter

out

=

new

outputstreamwriter(new

fileoutputstream(file,true),"utf-16le")//应该就是你要的不稿橡滑严谨的"键腊unicode"编码

import java.io.Fileimport java.io.IOExceptionpublic class CovertEncode { public static void main(String args[]) throws Exception { CovertFile("码好渗d:\袜氏\迟脊1.txt")} public static void CovertFile(String filename) throws IOException


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/8223689.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存