在Android中实现文件读写

55 篇文章 0 订阅
8 篇文章 0 订阅
android 实现下载文件

/**

  * 从网上下载

  *@param url 下载路径

  *@param outputFile 创建本地保存流的文件

  *@return

  * @return 下载失败返回1(比如没有网络等情况)下载成功返回0

  */

  public static int downloadFile(String urlPsth, File outputFile) {

  int result=0;

  try {

  URL url = new URL(urlPsth);

  HttpURLConnection conn =(HttpURLConnection) url.openConnection();

  conn.setDoInput(true);

  conn.connect();

  if( conn.getResponseCode() == HttpURLConnection.HTTP_OK)

  {

  InputStream is = conn.getInputStream();

  FileOutputStream fos = new FileOutputStream(outputFile);

  byte[] bt = new byte[1024];

  int i = 0;

  while ((i = is.read(bt)) > 0) {

  fos.write(bt, 0, i);

  }

  fos.flush();

  fos.close();

  is.close();

  }else {

  result=1;

  }

  } catch (FileNotFoundException e) {

  result=1;

  } catch (IOException e) {

  result=1;

  }

  return result;

  }


编程中文件读写是少不了的,如下:

读:
public String ReadSettings(Context context){
      FileInputStream fIn = null;
      InputStreamReader isr = null;
     
      char[] inputBuffer = new char[255];
      String data = null;
     
      try{
       fIn = openFileInput("settings.dat");      
          isr = new InputStreamReader(fIn);
          isr.read(inputBuffer);
          data = new String(inputBuffer);
          Toast.makeText(context, "Settings read",Toast.LENGTH_SHORT).show();
          }
          catch (Exception e) {      
          e.printStackTrace();
          Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show();
          }
          finally {
             try {
                    isr.close();
                    fIn.close();
                    } catch (IOException e) {
                    e.printStackTrace();
                    }
          }
          return data;
     }

写:
    public void WriteSettings(Context context, String data){
      FileOutputStream fOut = null;
      OutputStreamWriter osw = null;
     
      try{
       fOut = openFileOutput("settings.dat",MODE_PRIVATE);      
          osw = new OutputStreamWriter(fOut);
          osw.write(data);
          osw.flush();
          Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
          }
          catch (Exception e) {      
          e.printStackTrace();
          Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
          }
          finally {
             try {
                    osw.close();
                    fOut.close();
                    } catch (IOException e) {
                    e.printStackTrace();
                    }
          }
     }

使用方法:
WriteSettings(this,"setting0, setting1, setting2");
String data[] = ReadSettings(this).split(",");


在读取txt文件时,可能会遇到中文乱码情况,解决办法如下:

private String getTextString(String pathandname) throws IOException{
		
		String str="";
		
		FileInputStream fis = new FileInputStream(pathandname);
//		InputStreamReader isr=new InputStreamReader(fis, "gbk");
//		BufferedReader br=new BufferedReader(isr);
		
		int size=fis.available();
		
		byte[] buffer=new byte[size];
		
		fis.read(buffer);

		fis.close();
		   
		str = new String(buffer,"GBK");//支持双字节字符
		
		myApp.setCharNumofString(str.length());//存储总字符数
		
		return  str;
	}

读取assets文件夹下的txt文件
http://gundumw100.iteye.com/blog/850611
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android开发,文件操作是非常常见的,比如读取或写入文本、图片或视频等文件。本文将简要介绍在Android实现文件读写的一些基本知识和技巧。 1. 读取文件 在Android读取文件通常有两种常用的方法:使用FileInputStream和BufferedReader。如下所示: String filePath = "your/file/path"; // 使用FileInputStream读取 FileInputStream inputStream = new FileInputStream(filePath); byte[] buffer = new byte[1024]; int length = 0; while ((length = inputStream.read(buffer)) > 0) { String content = new String(buffer, 0, length); Log.i("File", "Content: " + content); } inputStream.close(); // 使用BufferedReader读取 FileReader fileReader = new FileReader(filePath); BufferedReader bufferedReader = new BufferedReader(fileReader); String line; while ((line = bufferedReader.readLine()) != null) { Log.i("File", "Line: " + line); } bufferedReader.close(); 2. 写入文件 如果需要向文件写入内容,我们可以使用FileOutputStream和BufferedWriter来实现。 String filePath = "your/file/path"; // 使用FileOutputStream写入 FileOutputStream outputStream = new FileOutputStream(filePath); String content = "Hello World!"; outputStream.write(content.getBytes()); outputStream.close(); // 使用BufferedWriter写入 FileWriter fileWriter = new FileWriter(filePath); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); String line = "Hello World!"; bufferedWriter.write(line); bufferedWriter.flush(); bufferedWriter.close(); 需要注意的是,在执行文件写入操作时,需要先授予应用程序写入文件的权限。可以在AndroidManifest.xml添加WRITE_EXTERNAL_STORAGE权限。 总结 以上是Android文件读写的基本内容。在实际开发,还需要注意文件路径的获取、文件不存在的判断等问题。另外,需要注意写入的权限授权和向用户说明应用程序写入的具体目的和内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值