android对SD卡文件的读写(FileOutputStream和FileinputStream的使用)

FileOutputStream和FileinputStream的使用

android 6.0以上的版本需要手动的写权限

这里实验的使用的是android4.4版本


第一步:编写布局文件




    
    

第二步:  给应用权限


    

第三步:写Mianactivity.java 


  3.1 定义对象


 

          private EditText edit;
          private Button btn1;
          private Button btn2;
         
          private boolean temp=false;
          private String text;
          
          private String FILENAME="/abcd.txt";

   3.2 思路要清晰,先写出 读写功能代码  

    按钮 btn1 设置监听


btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				temp=write(edit.getText().toString().trim());
				if(temp)
				{
					edit.setText("");
				Toast.makeText(MainActivity.this,"写入成功",Toast.LENGTH_SHORT).show();
				}else
				{
					Toast.makeText(MainActivity.this,"写入失败",Toast.LENGTH_SHORT).show();
				}
				
			}
		});
		
     

write方法 
 public boolean write(String content)
	{
		
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
		{
			
			try {
				
				/*File sdCardDir=Environment.getExternalStorageDirectory();
				File targetFile=new File(sdCardDir.getCanonicalPath()+NAME);
				
				RandomAccessFile raf=new RandomAccessFile(targetFile, "rw");
				raf.seek(targetFile.length());
				raf.write(content.getBytes());
				raf.close();*/
				File sdCardDir=Environment.getExternalStorageDirectory();
				File qst=new File(sdCardDir.getCanonicalPath()+FILENAME);
				
	
				
				FileOutputStream fos=new FileOutputStream(qst);  
				
				fos.write(content.getBytes());
				
			            
				fos.close();
				
			    /*  File sdCardDir = Environment.getExternalStorageDirectory();//获取SDCard目录
				   File saveFile = new File(sdCardDir, "a.txt");
				   FileOutputStream outStream = new FileOutputStream(saveFile);
				   outStream.write(content.getBytes());
				   outStream.close();*/
				
				/*File sdCardDir = new File("/sdcard"); //获取SDCard目录
				File saveFile = new File(sdCardDir, "itcast.txt"); 
				//上面两句代码可以合成一句: File saveFile = new File("/sdcard/a.txt");
				FileOutputStream outStream = new FileOutputStream(saveFile);
				outStream.write(content.getBytes());
				outStream.close();*/
				
				temp=true;
				return temp;
				
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			temp=false;
		}
		
		return temp;
		
	}

3.3 读取文件内容的代码

public boolean read()
	{
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
		{
			try{File sdCardDir=Environment.getExternalStorageDirectory();
			FileInputStream fis=new FileInputStream(sdCardDir.getCanonicalPath()+FILENAME);
			
			BufferedReader br=new BufferedReader(new InputStreamReader(fis));
			StringBuilder sb=new StringBuilder();
			String line=null;
			
			while((line=br.readLine())!=null)
			{
				sb.append(line);
			}
			text=sb.toString();			
			br.close();
			temp=true;
			
			}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		}else
		{
			temp=false;
		}
		
		return temp;
	}

最后给大家看一下Mianactivity的源码

public class MainActivity extends Activity {
         private EditText edit;
          private Button btn1;
          private Button btn2;
         
          private boolean temp=false;
          private String text;
          
          private String FILENAME="/abcd.txt";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		edit=(EditText)findViewById(R.id.edit);
		btn1=(Button)findViewById(R.id.btn1);
		btn2=(Button)findViewById(R.id.btn2);
		
		btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				temp=write(edit.getText().toString().trim());
				if(temp)
				{
					edit.setText("");
				Toast.makeText(MainActivity.this,"写入成功",Toast.LENGTH_SHORT).show();
				}else
				{
					Toast.makeText(MainActivity.this,"写入失败",Toast.LENGTH_SHORT).show();
				}
				
			}
		});
		
		btn2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				temp=read();
				if(temp)
				{
				edit.setText(text);
				Toast.makeText(MainActivity.this,"读入成功", Toast.LENGTH_SHORT).show();
				
				}else
				{
					Toast.makeText(MainActivity.this,"读取失败", Toast.LENGTH_SHORT).show();
				}
			}
		});
		
		
	}
	
	
	public boolean write(String content)
	{
		
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
		{
			
			try {
				
				/*File sdCardDir=Environment.getExternalStorageDirectory();
				File targetFile=new File(sdCardDir.getCanonicalPath()+NAME);
				
				RandomAccessFile raf=new RandomAccessFile(targetFile, "rw");
				raf.seek(targetFile.length());
				raf.write(content.getBytes());
				raf.close();*/
				File sdCardDir=Environment.getExternalStorageDirectory();
				File qst=new File(sdCardDir.getCanonicalPath()+FILENAME);
				
	
				
				FileOutputStream fos=new FileOutputStream(qst);  
				
				fos.write(content.getBytes());
				
			            
				fos.close();
				
			    /*  File sdCardDir = Environment.getExternalStorageDirectory();//获取SDCard目录
				   File saveFile = new File(sdCardDir, "a.txt");
				   FileOutputStream outStream = new FileOutputStream(saveFile);
				   outStream.write(content.getBytes());
				   outStream.close();*/
				
				/*File sdCardDir = new File("/sdcard"); //获取SDCard目录
				File saveFile = new File(sdCardDir, "itcast.txt"); 
				//上面两句代码可以合成一句: File saveFile = new File("/sdcard/a.txt");
				FileOutputStream outStream = new FileOutputStream(saveFile);
				outStream.write(content.getBytes());
				outStream.close();*/
				
				temp=true;
				return temp;
				
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			temp=false;
		}
		
		return temp;
		
	}
	
	public boolean read()
	{
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
		{
			try{File sdCardDir=Environment.getExternalStorageDirectory();
			FileInputStream fis=new FileInputStream(sdCardDir.getCanonicalPath()+FILENAME);
			
			BufferedReader br=new BufferedReader(new InputStreamReader(fis));
			StringBuilder sb=new StringBuilder();
			String line=null;
			
			while((line=br.readLine())!=null)
			{
				sb.append(line);
			}
			text=sb.toString();			
			br.close();
			temp=true;
			
			}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		}else
		{
			temp=false;
		}
		
		return temp;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

你可能感兴趣的:(安卓编程技术)