Java 瀛楃鐮併€佸瓧绗︽祦銆佸瓧鑺傛祦

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 瀛楃鐮併€佸瓧绗︽祦銆佸瓧鑺傛祦相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%ad%97%e7%ac%a6%e6%95%b0%e7%bb%84' title='瀛楃鏁扮粍'>瀛楃鏁扮粍   har   鏂囦欢   buffer   font   file   鍥剧墖   

瀛楃缂栫爜琛?/p>

Ascii: 0-xxxxxxx姝f暟

iso-8859-1: 鎷変竵鐮佽〃1-xxxxxxx  璐熸暟銆?/span>

GB2312: 绠€浣撲腑鏂囩爜琛?/p>

GBK: 鏈€甯哥敤鐨勪腑鏂囩爜琛?String瀛楃涓查粯璁?/span>

GB18030锛?/span> 鏈€鏂扮殑涓枃鐮佽〃

 

unicode锛?/span> 鍥介檯鏍囧噯鐮佽〃 char瀛楃榛樿 姣忎釜瀛楃涓ゅ瓧鑺?/span>

UTF-8: 鍩轰簬unicode 姣忎釜瀛楃涓€涓垨涓や釜瀛楄妭

 

鑳借瘑鍒腑鏂囩殑鐮佽〃锛?/span>GBK銆?/span>UTF-8

甯歌鐨勭紪鐮?/span> GBK  UTF-8  ISO-8859-1

鏂囧瓧--->(鏁板瓧) 锛氱紪鐮?鈥?/span>abc鈥?/span>.getBytes()  byte[]

(鏁板瓧)--->鏂囧瓧  : 瑙g爜 byte[] b={97,98,99}  new String(b,0,len)

ANSI:绯荤粺缂栫爜

 

銆€銆€銆€銆€銆€銆€瀛楄妭娴?In 銆€銆€銆€銆€銆€銆€銆€   瀛楃娴?/span>  瑕佸埛鏂癛eader

鎶借薄绫汇€€銆€ OutputStream 銆€銆€銆€銆€銆€銆€  銆€銆€Writer

銆€銆€銆€銆€銆€銆€銆€銆€|銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€ / 銆€              銆€銆€

瀹炵幇绫汇€€銆€ FileOutputStream銆€銆€ +銆€銆€ OutputStreamWriter = FileWriter

銆€銆€銆€銆€銆€銆€銆€銆€|銆€銆€銆€銆€銆€銆€ 銆€銆€銆€銆€ 缂栫爜銆€銆€銆€銆€銆€銆€銆€銆€ |

缂撳啿娴併€€銆€ BufferedOutputStream  PrintStream銆€銆€ 銆€銆€銆€銆€銆€BufferedWriter

 

瀛楄妭娴侊紙鍥剧墖锛?/p>

FileOutputStream  FileInputStream

 

public static void method01() throws IOException{

//FileOutputStream fos=new FileOutputStream(file);//浼犲叆File瀵硅薄

//FileOutputStream fos=new FileOutputStream("F:\a.txt");//鍒涘缓/瑕嗙洊

FileOutputStream fos=new FileOutputStream("F:\java\a.txt",true);/杩藉姞

fos.write(97); //鍐欏叆瀛楄妭   姝f暟锛?/span>Ascll鐮?a 璐熸暟锛氬崐涓眽瀛?/span> 鎴?/span>?

 

byte[] bytes={97,98,99,100};

fos.write(bytes); // 鍐欏叆涓€涓? 瀛楄妭 鏁扮粍

fos.write(bytes,1,1); // 浠?/span>1寮€濮? 闀垮害涓?/span>1

 

String str="浣犲ソ锛屼腑鍥?/span>2 "; // 鎹㈣锛?/span>

fos.write(str.getBytes()); // 瀛楃涓?杞?瀛楄妭鏁扮粍  鍐欏叆

fos.close();

}

 

瀛楃娴侊紙鏂囨湰锛屽彧鑳芥槸gbk鏍煎紡锛?/span>

 

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

//FileWriter fw = new FileWriter("d:\a.txt");//鍒涘缓杈撳嚭娴佸璞?/span>

//FileWriter fw = new FileWriter("b.txt");//椤圭洰璺緞涓?鍒涘缓鏂囦欢

FileWriter fw = new FileWriter("c.txt",true); //杩藉姞鍐欏叆=true

 

fw.write("helloworld");//鍐欏叆瀛楃涓?/span>

fw.write(" ");//windows:  linux:  mac:

//fw.flush();//瀛楃闇€瑕?鍒锋柊缂撳啿鍖?/span>

 

fw.write("abcde",0,5);//鍐欏叆瀛楃涓?璧峰绱㈠紩 闀垮害

fw.write(鈥榓鈥?;//鍐欏叆瀛楃char      a

fw.write(97);//鍐欏叆瀛楃 瀵瑰簲鐨?/span>Ascll鐮?   a

 

char[] chs = {鈥榓鈥?鈥榖鈥?鈥榗鈥?鈥榙鈥?鈥榚鈥榼;

fw.write(chs); //abcde

fw.write(chs,2,3);//鍐欏叆瀛楃鏁扮粍 璧峰绱㈠紩 闀垮害

 

fw.close();//鍒锋柊缂撳啿鍖?/span>+閲婃斁璧勬簮

}

以上是关于Java 瀛楃鐮併€佸瓧绗︽祦銆佸瓧鑺傛祦的主要内容,如果未能解决你的问题,请参考以下文章

鐩稿鍐呭瓨鐨勬枃浠舵搷浣滃瓧鑺傛祦

浜岃繘鍒?amp;瀛楃缂栫爜_20210306

鍑芥暟鑺傛祦

Java浠庡叆闂ㄥ埌瀹炴垬涔嬶紙20锛夊瓧绗︿覆涔?鏌ユ壘瀛楃涓叉渶鍚庝竴娆″嚭鐜扮殑浣嶇疆

Python str()鍑芥暟

PHP鍐呯疆鐨勫瓧绗︿覆澶勭悊鍑芥暟