zoukankan      html  css  js  c++  java
  • 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" );
    
            }
        
       
    
        }
    
    }
    

      

  • 相关阅读:
    PHP:第一章——PHP中字符运算符、比较运算符、错误控制运算符
    PHP:第一章——PHP中逻辑运算符的使用方法
    PHP:第一章——PHP中的算术运算符/递增、递减运算符/赋值运算符
    微信小程序通过js动态修改css样式的方法(交流QQ群:604788754)
    微信小程序跨页面获取数据示例
    JavaEE资源
    java 学习路线
    想以编程为职业,现在正在看毕向东的java基础,接下来应该看什么视频,求前辈们指教。
    2017Java学习路线图,内附完整Java自学视频教程+工具经验+面试
    各种 学习路线图专区
  • 原文地址:https://www.cnblogs.com/gisoracle/p/5238653.html
Copyright © 2011-2022 走看看