package com.backfire.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;

public class FeedSaveFile{
	FileOutputStream fos = null;
	static String FilePath="";
	String FileName="feedsave.txt";
	File file;
	String version;
	String channel;
	public FeedSaveFile(String filePath,String version,String channel){
		this.FilePath=filePath;
		this.version=version;
		this.channel=channel;
		 if (testSDCard()) {
		makcDir();
		createFile(FileName);
//		/writeFileData();
		//readFileData();
		 }
	}
	 public void writeFileData(){ 
		
	       try{ 
	    	  
	    	 fos = new FileOutputStream(file);
	    	   fos.write((version+","+channel).getBytes("utf-8"));
	    	   fos.close();
	        } 
	       catch(Exception e){ 

	        e.printStackTrace(); 

	       } 
		 }

	   
	 public String[] readFileData(){
		
		 String[] data=null;
		  String str="";
		 if(file.exists()){
		
		 //读取文件中的内容。可在程序中单独使用,不用关心"写"是否存在.
	        try    {
	            //只要f存在就可以读出f的内容,与写操作代码没有关联性.
	            FileInputStream in = new FileInputStream(file);
	            
	            byte[] buf = new byte[1024];    
	            
	            int len=in.read(buf);        //从流中读取内容
	            str = new String(buf,0,len);
	           
	            data=str.split(",");
	            
	            
	        }catch(Exception e)    {
	        	Log.e("文件：","出错了");
	        }
		 }
		 if(str==null||str.equals("")){
	        return new String[]{version,channel};
		 }
		 return data;
	 }
	 public void makcDir(){
		
				String str = getSDLogPath();
				File file = new File(str);
				
				boolean flag = true;
				
				if(!file.exists())
				{
					flag = file.mkdir();
				}
			
	 }
	 public static boolean testSDCard() {
			boolean flag = false;
			if (android.os.Environment.getExternalStorageState().equals(
					android.os.Environment.MEDIA_MOUNTED)) {
				flag = true;
			}
			return flag;
		}
	 public static String getSDLogPath() {
			return Environment.getExternalStorageDirectory().getAbsolutePath()
					+ File.separator + FilePath;
		}
	private  void createFile(String name) {
		String filepath=getSDLogPath();
		String fileName=filepath+File.separator+name;
		file = new File(fileName);
		if (file.exists())
			return;
		try {
			file.createNewFile();
		} catch (IOException e) {
		}
		
	}
}
