zoukankan      html  css  js  c++  java
  • Android 读取手机SD卡根目录下某个txt文件的文件内容

    1.先看activity_main.xml文件:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:android1="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <LinearLayout
    	    android:orientation="horizontal"
    	    android:layout_width="fill_parent"
    	    android:layout_height="wrap_content" >
    	    <TextView  
    		    android:textSize="18dip"
    		    android:layout_width="wrap_content" 
    		    android:layout_height="wrap_content" 
    		    android:text="文件名: /sdcard/" />
    	    <EditText 
    		    android:hint="xname.txt"
    		    android:id="@+id/ET_Folder" 
    		    android:layout_width="180dip" 
    		    android:layout_height="wrap_content" />
    	</LinearLayout>
    	    
        <LinearLayout
    	    android:orientation="horizontal"
    	    android:layout_width="fill_parent"
    	    android:layout_height="wrap_content" >
    	   <Button 
    		     android:text="打开" 
    		     android:id="@+id/But_Open" 
    		     android:layout_width="wrap_content"                                                        
    		     android:layout_height="wrap_content" />
    	   <Button 
    		     android:text="清除" 
    		     android:id="@+id/But_Clear" 
    		     android:layout_width="wrap_content"                                                        
    		     android:layout_height="wrap_content" />
    	</LinearLayout>
    
        <ScrollView 
    		android:id="@+id/ScrollView01" 
    		android:layout_width="fill_parent" 
    		android:layout_height="wrap_content">
    		<EditText 
    			android:editable="false"
    			android:id="@+id/ET_FileContent" 
    			android:layout_width="fill_parent" 
    			android:layout_height="wrap_content" />
       </ScrollView>
        
    </LinearLayout>

    2.MainActivity.java文件

    /*读取手机SD卡根目录下某个txt文件的文件内容 
     * */
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    import android.os.Bundle;
    import android.os.Environment;
    import android.app.Activity;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    	
    	private EditText et_folder;			//输入的文件夹名
    	private Button bt_open;				//打开按钮
    	private Button bt_clear;			//清除按钮
    	private EditText et_filecontent;	//用于显示txt文件内容
    	
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		
    		et_folder = (EditText) findViewById(R.id.ET_Folder);
            et_filecontent = (EditText) findViewById(R.id.ET_FileContent);
            
    		bt_open = (Button) findViewById(R.id.But_Open); 
    		bt_open.setOnClickListener(new OnClickListener(){//打开按钮监听
    			public void onClick(View arg0) {
    				//若输入的文件夹名为空
    				if(et_folder.getText().toString().trim().equals("")){
    					Toast.makeText(getApplicationContext(), "输入为空",
    							Toast.LENGTH_SHORT).show();
    				}else{
    					//获得SD卡根目录路径   "/sdcard"
    					File sdDir = Environment.getExternalStorageDirectory();
    					//根目录下某个txt文件名
    					File path = new File(sdDir+File.separator
    					 +et_folder.getText().toString().trim());
    					
    					// 判断SD卡是否存在,并且是否具有读写权限
    					if (Environment.getExternalStorageState()
    						.equals(Environment.MEDIA_MOUNTED)) {  
    			            et_filecontent.setText("");
    			            
    			            et_filecontent.setText(getFileContent(path));
    					}
    				}
    			}
    		});
    		
    		bt_clear = (Button) findViewById(R.id.But_Clear); 
    		bt_clear.setOnClickListener(new OnClickListener(){//清除按钮监听
    			public void onClick(View arg0) {
    				et_folder.setText("");
    	            et_filecontent.setText("");				
    			}
    		});
    		
    	}
    	
    	//读取指定目录下的所有TXT文件的文件内容
    	protected String getFileContent(File file) {
    		String content  = "";
    		if (file.isDirectory() ) {	//检查此路径名的文件是否是一个目录(文件夹)
                Log.i("zeng", "The File doesn't not exist "
                	+file.getName().toString()+file.getPath().toString());   
            } else {
                if (file.getName().endsWith(".txt")) {//文件格式为txt文件
                	try {
                        InputStream instream = new FileInputStream(file); 
                        if (instream != null) {
                            InputStreamReader inputreader
                            	=new InputStreamReader(instream, "GBK");
                            BufferedReader buffreader = new BufferedReader(inputreader);
                            String line="";
                            //分行读取
                           while (( line = buffreader.readLine()) != null) {
                                content += line + "
    ";
                            }                
                            instream.close();		//关闭输入流
                        }
                    }
                    catch (java.io.FileNotFoundException e) {
                        Log.d("TestFile", "The File doesn't not exist.");
                    } 
                    catch (IOException e)  {
                         Log.d("TestFile", e.getMessage());
                    }
                }
    		}
    		return content ;
    	}
    }
    



  • 相关阅读:
    [Oracle][Partition][Controlfile]Partition 操作是否和 Controlfile有关?
    [Oracle]如何观察Table 的各种Lock 之间的冲突
    [Oralce][InMemory]如何确定一个表已经被Populate 到In Memory 中?
    Arduino和C51之串口通信
    XC文件管理器-打造优美易用的文件管理器
    Android 控件进阶修炼-仿360手机卫士波浪球进度控件
    Android控件进阶-自定义流式布局和热门标签控件
    Windows开发之VC++仿QQ迷你首页(迷你资讯)
    Android自定义控件进阶-打造Android自定义的下拉列表框控件
    Android 自定义控件-高仿猎豹清理大师自定义内存开口圆环控件
  • 原文地址:https://www.cnblogs.com/anyuan9/p/6171540.html
Copyright © 2011-2022 走看看