• 文件编码的测试(android)


    做了一个文件编码的测试,为下一个作品做准备,需要准备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
            
        }
    }
  • 相关阅读:
    分布式系统知识点六:分布式锁及其简单实现讲解 2 zookpeer的实现(转载)
    分布式系统知识点六:分布式锁及其简单实现讲解 1redis的实现(转载)
    分布式系统知识点五:分布式事务及其解决方案2 利用消息队列处理分布式事务(转载)
    分布式系统知识点五:分布式事务及其解决方案 1 2PC,3PC简洁(转载)
    分布式系统知识点四: 一致性hash介绍 (转载)
    分布式系统知识点三: Paxos知识2 Paxos实现简洁及MulitPaxos介绍 (转载)
    分布式系统知识点三: Paxos知识1 算法原理与推导 (转载)
    分布式系统知识点二: 分布式一致性模型分类 (转载)
    分布式系统知识点一: CAP理论 (转载)
    压缩目录下的文件
  • 原文地址:https://www.cnblogs.com/zziss/p/2372638.html
Copyright © 2020-2023  润新知