包名下存储

package com.bawei.view;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

	}

	// 读取 Cache文件中的数据
	public void readCache(View v) {
		
		File file=new File(getCacheDir(),"cachers.txt");
		try {
			FileReader fr=new FileReader(file);
			char[] buf=new char[1024];
			while(true){
				int i=fr.read(buf);
				if(i!=-1){
					System.out.println(new String(buf,0,i));//读取到的数据,转换成字符串从哪里开始到哪里结束
				}else{
					break;
				}
			}
			fr.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
//		// 得到当前Cache文件的路径和文件的名字
//		File file = new File(getCacheDir(), "cache.txt");
//		try {
//			FileReader fr = new FileReader(file);
//			char[] buf = new char[1024];
//			while (true) {
//				int i = fr.read(buf);
//				if (i != -1) {
//					System.out.println(new String(buf, 0, i));
//				} else {
//					break;
//				}
//			}
//			fr.close();
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}

	}

	// 保存到Cache中
	public void saveCache(View v) {
		File file=new File(getCacheDir(),"cachers.txt");
		
		
		try {
			
			//打印流
			PrintWriter pw=new PrintWriter(file);
			pw.println("大家好!!!");
			pw.flush();
			pw.close();
			
			
//			FileWriter fw=new FileWriter(file,true);
//			
//			fw.write("你好!!!");
//			
//			fw.flush();
//			
//			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
//		// 得到当前Cache文件的路径 data/data/包名/Cache
//		File file = new File(getCacheDir(), "cache.txt");
//
//		try {
//			FileWriter fw = new FileWriter(file);
//			fw.write("宁小丫");
//			fw.flush();
//			fw.close();
//
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}

	// 保存到file中
	public void saveFile(View v) {
		try {
			FileOutputStream fos=openFileOutput("file.txt", MODE_PRIVATE);
			
			fos.write("Hello World!!!".getBytes());
			
			fos.flush();
			
			fos.close();
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		
//		try {
//			FileOutputStream fis = openFileOutput("files.txt", MODE_PRIVATE);
//			fis.write("宁小丫".getBytes());
//			fis.flush();
//			fis.close();
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}

	// 读取file文件中的数据
	public void readFile(View v) {
		
		try {
			//缓冲流
			FileInputStream fis=openFileInput("file.txt");
			BufferedReader br=new BufferedReader(new InputStreamReader(fis));
			
			String str=br.readLine();
			System.out.println(str);
			
//			byte[] buffer=new byte[1024];
//			
//			while(true){
//				int i=fis.read(buffer);
//				
//				if(i!=-1){
//					System.out.println(new String(buffer,0,i));
//				}else{
//					break;
//				}
//
//			}
			
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
//		try {
//			//打开指定文件名的文件
//			FileInputStream fis = openFileInput("files.txt");
//			byte[] buf = new byte[1024];
//			while (true) {
//				int i = fis.read(buf);
//				if (i != -1) {
//					System.out.println(new String(buf, 0, i));
//
//				} else {
//					break;
//				}
//			}
//
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值