Java基礎二十

來源:互聯網
上載者:User

標籤:[]   流轉   ade   字元   import   流操作   pac   子類   put   

1 轉換流1.1 轉換流出現的原因
  • 由於位元組流操作中文的時候不是很方便,所以,Java就提供了轉換流。
  • 字元流=位元組流+編碼錶。
1.2 String類的編碼和解碼功能
  • 方法:通過指定的編碼,將二進位的資料轉換為字串,即解碼
String(byte[] bytes, Charset charset) 

 

  • 方法:通過制定的編碼,將字串轉換為二進位的資料,即編碼
public byte[] getBytes(Charset charset)

 

  • 樣本:
package com.xuweiwei;import java.io.UnsupportedEncodingException;public class StringDemo {    public static void main(String[] args) throws UnsupportedEncodingException {        String str = "abcdef";        //編碼        byte[] bytes = str.getBytes("UTF-8");        for (byte aByte : bytes) {            System.out.println("aByte = " + aByte +"、" );        }        //解碼        System.out.println("解碼:"+new String(bytes,"UTF-8"));    }}

 

2 OutputStreamWriter
  • 作用:將位元組輸出資料流轉換為字元輸出資料流。

 

  • 構造方法:根據指定的編碼將位元組流轉換為字元流
public OutputStreamWriter(OutputStream out, String charsetName) throws UnsupportedEncodingException

 

  • 樣本:
package com.xuweiwei;import java.io.*;public class OutputStreamWriterDemo {    public static void main(String[] args) throws IOException {        Writer ow = new OutputStreamWriter(new FileOutputStream("ow.txt"),"utf-8");        ow.write("你好,許威威");        ow.close();    }}

 

3 InputStreamReader
  • 將位元組輸入資料流轉換為字元輸入資料流

 

  • 構造方法:
public InputStreamReader(InputStream in, String charsetName) throws UnsupportedEncodingException

 

  • 樣本:
package com.xuweiwei;import java.io.*;public class InputStreamReaderDemo {    public static void main(String[] args) throws IOException {        Reader reader = new InputStreamReader(new FileInputStream("ow.txt"),"utf-8");        char[] buffer = new char[1024];                int len = 0;        while(-1 != (len = reader.read(buffer))){            System.out.println(new String(buffer,0,len));        }        reader.close();    }}

 

4 轉換流的簡化寫法
  • 抓換流的名字比較長,而我們常見的操作都是按照本地預設編碼實現的,所以, 為了簡化我們的書寫,轉換流就提供了對應的子類。
  • 例如:FileWriter和FileReader。

 

  • 樣本:
package com.xuweiwei;import java.io.FileWriter;import java.io.IOException;public class FileWriterDemo {    public static void main(String[] args) throws IOException {        FileWriter fw = new FileWriter("fw.txt");        fw.write("你好啊");        fw.close();    }}

 

5 字元緩衝流
  • BufferedWriter
    • 特殊功能:寫一個行分隔字元
public void newLine()   throws IOException
  • BufferedReader
    • 特殊功能:讀一個行分隔字元  
public String readLine()

 

  • 樣本:
package com.xuweiwei;import java.io.*;public class BufferedWriterDemo {    public static void main(String[] args) throws IOException {        BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));        bw.write("你好啊");        bw.flush();        bw.close();    }}

 

  • 樣本:
package com.xuweiwei;import java.io.FileWriter;import java.io.IOException;public class FileWriterDemo {    public static void main(String[] args) throws IOException {        FileWriter fw = new FileWriter("fw.txt");        fw.write("你好啊");        fw.close();    }}

 

  • 樣本:
package com.xuweiwei;import java.io.*;public class CopyDemo {    public static void main(String[] args) throws IOException {        BufferedReader br = new BufferedReader(new FileReader("br.txt"));        BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));        String line = null;        while(null != (line = br.readLine())){            bw.write(line);            bw.newLine();            bw.flush();        }        bw.close();        br.close();    }}

 

Java基礎二十

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.