android--简单的文件创建与读写操作

public File createSDFile(String fileName) throws IOException { 
		File file = new File(SDPATH + "//" + fileName); 
		if (!file.exists()) { 
		file.createNewFile(); 
		} 
		return file; 
		} 
	public void writeSDFile(String str,String fileName) 
	{ 
		try
	      {
	       FileOutputStream outStream = new FileOutputStream("/mnt/sdcard/"+fileName,true);
	       OutputStreamWriter writer = new OutputStreamWriter(outStream,"utf-8");
	       writer.write(str);
	       writer.write("\r\n");
	       writer.flush();
	       writer.close();//记得关闭


	       outStream.close();
	      }
	      catch (Exception e)
	      {
	       Log.e("m", "file write error");
	      } 
	} 
	 public String readFileSdcard(String fileName) 
	 { 	 
	     String res = ""; 	 
	     try 
	     { 	 
	         FileInputStream fin = new FileInputStream(fileName); 	 
	         int length = fin.available();  
	         byte[] buffer = new byte[length]; 	 
	         fin.read(buffer); 	 
	         res = EncodingUtils.getString(buffer, "UTF-8"); 	 
	         fin.close(); 	 
	     } 	 
	     catch (Exception e) 
	     { 	 
	         e.printStackTrace(); 	 
	     } 	 
	     return res; 	 
	 } 


你可能感兴趣的:(android,读写,文件创建)