android 讀中文文字檔,android中文

來源:互聯網
上載者:User

android 讀中文文字檔,android中文

AndroidManifest.xml中

加入:

   <!-- 在SDCard中建立與刪除檔案許可權 -->    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>    <!-- 往SDCard寫入資料許可權 -->    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
package com.example.yanlei.wifi;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.os.Environment;import android.support.v7.app.AppCompatActivity;import android.text.method.ScrollingMovementMethod;import android.view.Menu;import android.widget.TextView;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import jxl.Cell;import jxl.Sheet;import jxl.Workbook;public class MainActivity extends AppCompatActivity {    TextView txt = null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        txt = (TextView)findViewById(R.id.txt_show);        txt.setMovementMethod(ScrollingMovementMethod.getInstance());        ShowMessage("ok2");        //readtxt();        readExcel();        ShowMessage("ok3");    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        //getMenuInflater().inflate(R.menu.main, menu);        return true;    }    /**     * 擷取內建SD卡路徑     *     * @return     */    public String getInnerSDCardPath() {        File sdDir = null;        boolean sdCardExist = Environment.getExternalStorageState()                .equals(android.os.Environment.MEDIA_MOUNTED);   //判斷sd卡是否存在        if (sdCardExist) {            sdDir = Environment.getExternalStorageDirectory();//擷取跟目錄        }        String Path = sdDir.toString();        return Path;    }    public String getSDPath() {        List<String> lResult = new ArrayList<String>();        try {            Runtime rt = Runtime.getRuntime();            Process proc = rt.exec("mount");            InputStream is = proc.getInputStream();            InputStreamReader isr = new InputStreamReader(is);            BufferedReader br = new BufferedReader(isr);            String line;            while ((line = br.readLine()) != null) {                if (line.contains("extSdCard")) {                    String[] arr = line.split(" ");                    String path = arr[1];                    File file = new File(path);                    if (file.isDirectory()) {                        lResult.add(path);                    }                }            }            isr.close();        } catch (Exception e) {        }        int num=lResult.size();        //ShowMessage("外盤的個數:"+num);        if (num>0) {            return lResult.get(0).toString();        }        else        {            return "";        }    }    /* @param path 檔案夾路徑       */    public static boolean isExist(String path) {        File file = new File(path);        //判斷檔案夾是否存在,如果不存在則建立檔案夾        if (!file.exists()) {            //file.mkdir();            return false;        }        return true;    }    //判斷檔案是否存在    public boolean fileIsExists(String strFile) {        try {            File f = new File(strFile);            if (!f.exists()) {                return false;            }        } catch (Exception e) {            return false;        }        return true;    }    public void ShowMessage(String str) { //        new AlertDialog.Builder(this)                .setMessage(str)                .setPositiveButton("確定",                        new DialogInterface.OnClickListener() {                            public void onClick(DialogInterface dialoginterface, int i) {                                //按鈕事件                            }                        })                .show();    }    public String getFilePath() {        String path = getSDPath();        if (path!="") {            if (isExist(path + "/qqq.txt")) {                return path;            }        }        path = getInnerSDCardPath();        if (isExist(path + "/qqq.txt")) {            return path;        }        return path;    }    public static String readFile(String filePathAndName) {        String fileContent = "";        try {            File f = new File(filePathAndName);            if(f.isFile()&&f.exists()){                InputStreamReader read = new InputStreamReader(new FileInputStream(f),"gbk");                BufferedReader reader=new BufferedReader(read);                String line;                while ((line = reader.readLine()) != null) {                    fileContent += line;                }                read.close();            }        } catch (Exception e) {            System.out.println("讀取檔案內容操作出錯");            e.printStackTrace();        }        return fileContent;    }    public void readtxt() {        {            String path=getFilePath();            ShowMessage("路徑:"+path);            if (path=="")            {                ShowMessage("檔案:"+path+"/qqq.txt"+"不存在");                return;            }            ShowMessage("11111111111111111111" );            ShowMessage(readFile(path + "/qqq.txt"));            ShowMessage("222222222" );        }           }}

  

聯繫我們

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