外部储存篇

实习第二周,这是第一篇实习总结

以前也不少使用File储存,这不刚来公司,就被一个小小的Demo困了一下午,下午上班前,经理让我读取外网IP,IMEI,然后退出程序,进来再次读取,判断和上次读取的数据是否相同

自然读取IP和IMEI网上就有大把教程,我这里就不缀述,这里是来归纳外部储存的一些使用心得

自己分了类型

1.每次储存都覆盖上一次文件。

2.每一次储存都把数据追加到上一次文件上。

大伙可以根据需求自行选择,我把代码贴上来

覆盖篇

public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Toast.makeText(MainActivity.this, readOldIMEI(), Toast.LENGTH_SHORT).show();
		
	}
	//获得读写公用的外部储存路径
	/**
	 * 
	 * @param fileName
	 *            文件名
	 * @return
	 */
	private File mCacheFile;
	private String directoryName="text";//目录名
	public File getFile(String fileName) {
		if (mCacheFile == null) {
			mCacheFile = new File(Environment.getExternalStorageDirectory(), directoryName);
			
		}
		File file2 = new File(mCacheFile, fileName);
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			//判断目录或目录文件是否存在
			if (!mCacheFile.exists()) {
				mCacheFile.mkdirs();//创建目录
			}
			try {
				file2.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return file2;
	}
	String newIMEI="84545454654564";
	//将数据写入外部储存
	private void saveIMEI(String newIMEI) {
		File file2 = getFile("IMEI");
		if (file2.exists()) {
			try {
				FileOutputStream fos = new FileOutputStream(file2);
				fos.write(newIMEI.getBytes());
				fos.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	@Override
		protected void onPause() {
			// TODO Auto-generated method stub
			super.onPause();
			saveIMEI(newIMEI);
		}
	/**
	 * 
	 * @return 读取上次IMEI
	 */
	private String readOldIMEI() {
		File file2 = getFile("IMEI");
		String text = null;
		if (file2.exists()) {
			FileInputStream fis;
			try {
				fis = new FileInputStream(file2);
				BufferedReader br = new BufferedReader(new InputStreamReader(fis));
				text = br.readLine();
				br.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return text;
	}

	
}
追加篇

public class MainActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Toast.makeText(MainActivity.this, Readerimei(), Toast.LENGTH_SHORT).show();
		
	}

	public void saveimei(String content)

	{

		try {
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            	
                //获取SD卡的目录
                File sdCardDir = Environment.getExternalStorageDirectory();
                File targetFile = new File(sdCardDir.getCanonicalPath() + "/resultimei.txt");
                //以指定文件创建RandomAccessFile对象
                RandomAccessFile raf = new RandomAccessFile(targetFile, "rw");
                //将文件记录指针移动到最后
                raf.seek(targetFile.length());
                //输出文件内容
                raf.write(content.getBytes());
                raf.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

	}
	public String Readerimei() {
		try {
			 
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            	Toast.makeText(MainActivity.this, "SD卡存在", Toast.LENGTH_SHORT).show();
                //获得SD卡对应的存储目录
                File sdCardDir = Environment.getExternalStorageDirectory();
                String str = sdCardDir.getCanonicalPath() + "resultimei.txt";
                //获取指定文件对应的输入流
                FileInputStream fis = new FileInputStream(sdCardDir.getCanonicalPath() + "/resultimei.txt");
                //将指定输入流包装成BufferReader
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                StringBuilder sb = new StringBuilder("");
                String line = null;
                //循环读取文件内容
                while((line = br.readLine()) != null){
                    sb.append(line);
                }
                br.close();
                return sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
	 return null;
	}
	String IMEI="844646546546546\n";
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		saveimei(IMEI);
	}

	
}

还有很多方法,自己就总结这两种供以后使用了

你可能感兴趣的:(工作小记)