• 文件编码的测试(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
            
        }
    }
  • 相关阅读:
    【PaddlePaddle系列】Executor逐步训练模型
    【PaddlePaddle】自然语言处理:句词预测
    【PaddlePaddle系列】CIFAR-10图像分类
    【图像处理】灰度图、亮度峰值极值查找
    【PaddlePaddle系列】报错解决方法合集 (不定时更新)
    【PaddlePaddle系列】手写数字识别
    Thymeleaf 基本用法总结
    Eclipse+Marven + spring mvc 新建一个 Hello world 项目
    Java EE 开发环境搭建
    vue.js安装过程(npm安装)
  • 原文地址:https://www.cnblogs.com/zziss/p/2372638.html
Copyright © 2020-2023  润新知