Android(输入输出流的使用)详解

bt4.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
					try {
						OutputStream out =openFileOutput("ren.txt",Context.MODE_PRIVATE);
						String info="我是中国人";
						byte[] bytes=info.getBytes();
						out.write(bytes,0,bytes.length);//写入
						out.close();

						
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
			}
		});
		
		
		bt5.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
				
				try {
					InputStream in=openFileInput("ren.txt");
					byte[] bytes=new byte[1024];
					StringBuffer sb=new StringBuffer();
					int len =-1;
					while ((len=in.read(bytes))!=-1) {
						sb.append(new String(bytes,0,len));
					}
					in.close();
					Toast.makeText(MainActivity.this,sb.toString(),0).show();
					
					
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});

你可能感兴趣的:(Android(输入输出流的使用)详解)