安卓如何將TXT文件寫到特定路徑


其實就一個方法,就不貼所有代碼了。

    /**
     * 寫入文件方法
     * @param content
     */
    public static void write(String content) {
        try {
            //判斷實際是否有SD卡,且應用程序是否有讀寫SD卡的能力,有則返回true
            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                // 獲取SD卡的目錄
                File sdCardDir = Environment.getExternalStorageDirectory();
                String path = "/APP/";
                File dir = new File(sdCardDir+path);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                File targetFile = new File(sdCardDir.getCanonicalPath() + path+"aaa.txt");
                //使用RandomAccessFile是在原有的文件基礎之上追加內容,
                //而使用outputstream則是要先清空內容再寫入
                RandomAccessFile raf = new RandomAccessFile(targetFile, "rw");
                //光標移到原始文件最后,再執行寫入
                raf.seek(targetFile.length());
                raf.write(content.getBytes());
                raf.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

package com.example.linfeng.myapplication;

import android.os.Environment;

import java.io.File;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by LinFeng on 2017/11/13.
 */

public class TextHelper {


    public static void write(String content, String PathName, String FileName) {
        try {
            //判斷實際是否有SD卡,且應用程序是否有讀寫SD卡的能力,有則返回true

            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                // 獲取SD卡的目錄
                File sdCardDir = Environment.getExternalStorageDirectory();
                //Log.i("paht",sdCardDir.toString());
                String path = "/" + PathName + "/";
                //如果不存在,就創建目錄
                File dir = new File(sdCardDir + path);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                File targetFile = new File(sdCardDir.getCanonicalPath() + path + FileName + ".dat");
                //使用RandomAccessFile是在原有的文件基礎之上追加內容,
                //而使用outputstream則是要先清空內容再寫入
                RandomAccessFile raf = new RandomAccessFile(targetFile, "rw");
                //光標移到原始文件最后,再執行寫入
                raf.seek(targetFile.length());
                raf.write(content.getBytes());
                raf.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //寫頭文件,如果不存在寫,存在就不寫
    public  static  void writeHead(String path,String fileName,char type){

        String string=Environment.getExternalStorageDirectory()+"/" + path + "/"+fileName+".dat";
        if (!fileIsExists(string)){
           switch (type){
               case 'I':
                   String content = "Type=Inventory"+"\r\n"+"Date="+getStringDate()+"\r\n"+"ScannerNo=01";
                   write(content,path,fileName);
                   break;
               case 'M':
                   write("Type=Market"+"\r\n",path,fileName);
                   break;
               case 'R':
                   write("Type=Return"+"\r\n",path,fileName);
                   break;
           }
        }else{
            
        }
    }

    //判斷文件是否存在
    public static boolean fileIsExists(String strFile) {
        try {
            File f = new File(strFile);
            if (!f.exists()) {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }


    //獲取當前時間,格式為:yyyyMMdd HH:mm:ss
    public static String getStringDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM