最近有个想法,就是想实现一个读写文件实例,一开始只想读写简单的文件(如txt格式文件),后面想到了读写XML文件,其实无论是写txt文件还是XML文件,其实写入的值都是一个字符串值,所以关键是如何实现读写sdcard里面的文件。

下面我讲一下如何如写和读txt文件,其实XML文件同样,只不过在写和读之前还要做相应的处理而已

  如果你要写SDcard文件,首先你就要有写sdcard操作权限。即要在AndroidManifest.xml文件中加入申请权限

   

    <!-- 讀寫SDK卡一定要申請權限允可 -->

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

   然后你就可以进行写和读操作了。

 

        /**

     * 寫入指定XML文件中的內容

     * @param file  文件

     * @param destDirStr  文件目錄

     * @param szOutText  文件內容

     * @return

     */

    public String writeToSDcardFile(String file, String destDirStr,

            String szOutText) {

        // 获取扩展SD卡设备状态

        String sDStateString = android.os.Environment.getExternalStorageState();


        File myFile = null;

        // 拥有可读可写权限

        if (sDStateString.equals(android.os.Environment.MEDIA_MOUNTED)) {


            try {


                // 获取扩展存储设备的文件目录

                File SDFile = android.os.Environment

                        .getExternalStorageDirectory();


                // File destDir=new File("/sdcard/xmlfile");


                File destDir = new File(SDFile.getAbsolutePath() + destDirStr);


                if (!destDir.exists())

                    destDir.mkdir();

                // Toast.makeText(SDCardTest., text, duration)

                // 打开文件

                myFile = new File(destDir + File.separator + file);


                // 判断是否存在,不存在则创建

                if (!myFile.exists()) {

                    myFile.createNewFile();

                }


                // 写数据

                // String szOutText = "Hello, World!";

                FileOutputStream outputStream = new FileOutputStream(myFile);

                outputStream.write(szOutText.getBytes());

                outputStream.close();


            } catch (Exception e) {

                // TODO: handle exception

            }// end of try


        }// end of if(MEDIA_MOUNTED)

        // 拥有只读权限

        else if (sDStateString

                .endsWith(android.os.Environment.MEDIA_MOUNTED_READ_ONLY)) {


            // 获取扩展存储设备的文件目录

            File SDFile = android.os.Environment.getExternalStorageDirectory();


            // 创建一个文件

            myFile = new File(SDFile.getAbsolutePath() + destDirStr

                    + File.separator + file);


            // 判断文件是否存在,存在的情況下才去讀該文件

            if (myFile.exists()) {

                try {


                    // 读数据

                    FileInputStream inputStream = new FileInputStream(myFile);

                    byte[] buffer = new byte[1024];

                    inputStream.read(buffer);

                    inputStream.close();


                } catch (Exception e) {

                    // TODO: handle exception

                }// end of try

            }// end of if(myFile)

        }// end of if(MEDIA_MOUNTED_READ_ONLY)

        // end of func


        return myFile.toString();

    }

   

    /**

     * 讀出指定XML文件中的內容

     * @param file  文件

     * @param destDirStr  文件目錄

     * @param szOutText  文件內容

     * @return 返回文件內容

     */

    public String readContentFromFile(String file, String destDirStr){

       

        String content=null;

        // 获取扩展存储设备的文件目录

        File SDFile = android.os.Environment.getExternalStorageDirectory();


        // 创建一个文件

        File myFile = new File(SDFile.getAbsolutePath() + destDirStr

                + File.separator + file);


        // 判断文件是否存在,存在的情況下才去讀該文件

        if (myFile.exists()) {

            try {


                // 读数据

                FileInputStream inputStream = new FileInputStream(myFile);

                byte[] buffer = new byte[1024];

                inputStream.read(buffer);

                inputStream.close();

               

                content=new String(buffer);

               


            } catch (Exception e) {

                // TODO: handle exception

            }// end of try

        }// end of if(myFile)

       

        return content;

    }

然后我们在外部类实现调用

写入指定文件:

SDCardOperate sdOperate=new SDCardOperate();

                sdOperate.writeToSDcardFile("test.xml","/xmlfiles/","helloworld" );

//XMLWriterOperate.writeXml()

从指定文件中读出:

SDCardOperate sdOperate=new SDCardOperate();

                String content=sdOperate.readContentFromFile("test.xml","/xmlfiles/");

               

                Toast.makeText(SQL2.this, content, Toast.LENGTH_LONG).show();