檔案編碼的測試(android)

來源:互聯網
上載者:User

做了一個檔案編碼的測試,為下一個作品做準備,需要準備4個不同編碼的檔案

在 code 中已指明了檔案名稱

 

xml 代碼如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   
    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <TextView
        android:id="@+id/tv2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <Button 
        android:id="@+id/btn_utf8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="utf8"
        />
    <Button 
        android:id="@+id/btn_un"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="unicode"
        />
    
    <Button 
        android:id="@+id/btn_ansi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ansi"
        />
    <Button 
        android:id="@+id/btn_bigunicode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bigunicode"
        />
</LinearLayout>

 

java 代碼

 

package zziss.android.txt;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TxttestActivity extends Activity {
    /** Called when the activity is first created. */
    
    private Button btn_utf8;
    private Button btn_ansi;
    private Button btn_unicode;
    private Button btn_bigunicode;
    private TextView tv;
    private String fpath;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        fpath = "/data/"+Environment.getDataDirectory().getAbsolutePath()+"/zziss.android.txt/";
        
        btn_utf8 = (Button)this.findViewById(R.id.btn_utf8);
        btn_ansi = (Button)this.findViewById(R.id.btn_ansi);
        btn_unicode = (Button)this.findViewById(R.id.btn_un);
        btn_bigunicode = (Button)this.findViewById(R.id.btn_bigunicode);
        tv = (TextView)this.findViewById(R.id.tv);
        btn_utf8.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String fname  = fpath + "utf8.txt";
                File file = new File(fname);
                try {
                    /*Reader reader = new FileReader(file);
                    char[] c = new char[100];
                    reader.read(c);*/
                    InputStream reader = new FileInputStream(file);
                    byte[] b = new byte[reader.available()];
                    reader.read(b);
                    // 這個也不可以轉成 gbk
                    String s = new String(b,"utf-8");
                    tv.setText(s);
                    
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }catch (IOException e) {
                    e.printStackTrace();
                }
                
            }
        });  // btn_utf8
        
        btn_ansi.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String fname  = fpath + "ansi.txt";
                File file = new File(fname);
                try {
                    /*Reader reader = new FileReader(file);
                    char[] c = new char[100];
                    reader.read(c);
                    String s = String.valueOf(c);*/
                    InputStream reader = new FileInputStream(file);
                    byte[] b = new byte[100];
                    reader.read(b);
                    // ansi的我一直使用 iso8859-1 或 us-ascii ,但都是亂碼,換成 gbk 就 ok 了
                    String s = new String(b,"GBK");
                    tv.setText(s);
                    
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }catch (IOException e) {
                    e.printStackTrace();
                }
                
            }
        });  // btn_utf8
        
        btn_unicode.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String fname  = fpath + "unicode.txt";
                File file = new File(fname);
                try {
                    /*Reader reader = new FileReader(file);
                    char[] c = new char[100];
                    reader.read(c);
                    String s = String.valueOf(c);*/
                    InputStream reader = new FileInputStream(file);
                    byte[] b = new byte[reader.available()];
                    reader.read(b);
                    // utf-16 的就不可以轉成 gbk
                    String s = new String(b,"utf-16");
                    tv.setText(s);
                    
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }catch (IOException e) {
                    e.printStackTrace();
                }
                
            }
        });  // unicode
        
        btn_bigunicode.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String fname  = fpath + "bigunicode.txt";
                File file = new File(fname);
                try {
                    /*Reader reader = new FileReader(file);
                    char[] c = new char[100];
                    reader.read(c);
                    String s = String.valueOf(c);*/
                    InputStream reader = new FileInputStream(file);
                    byte[] b = new byte[reader.available()];
                    reader.read(b);
                    // 對於 bigunicode ,轉成 gbk 就可以
                    String s = new String(b,"GBK");
                    tv.setText(s);
                    
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }catch (IOException e) {
                    e.printStackTrace();
                }
                
            }
        });  // btn_bigunicode
        
    }
}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.