zoukankan      html  css  js  c++  java
  • android手机操作SD的使用方法

    写入SD卡

    package com.example.openfileproject;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.PrintStream;
    import java.util.Scanner;

    import android.os.Bundle;
    import android.os.Environment;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity {
    private static final String FILENAME="liyuanjing.txt";//文件名
    private static final String DIR="liyuanjing";//文件夹名
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.activity_main);
    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//SD存在时
    File file=new File(Environment.getExternalStorageDirectory().toString()
    +File.separator+DIR
    +File.separator+FILENAME);
    if(!file.getParentFile().exists()){//如果文件夹不存在
    file.getParentFile().mkdirs();//创建文件夹
    }
    PrintStream out=null;//打印流对象用于输出
    try {
    out=new PrintStream(new FileOutputStream(file));//追加文件
    out.println("http://weibo.com/liyuanjinglyj");
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    if(out!=null){
    out.close();//关闭打印流
    }
    }
    }else{//使用Toast提示用户SD卡不存在
    Toast.makeText(this, "SD卡不村在", Toast.LENGTH_LONG).show();
    }

    }

    @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;
    }

    }

    从SD卡读数据

    package com.example.openfileproject;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.PrintStream;
    import java.util.Scanner;

    import android.os.Bundle;
    import android.os.Environment;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity {
    private static final String FILENAME="liyuanjing.txt";//文件名
    private static final String DIR="liyuanjing";//文件夹名
    private TextView txt=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.activity_main);
    this.txt=(TextView)super.findViewById(R.id.txt);
    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//SD存在时
    File file=new File(Environment.getExternalStorageDirectory().toString()
    +File.separator+DIR
    +File.separator+FILENAME);
    if(!file.getParentFile().exists()){//如果文件夹不存在
    file.getParentFile().mkdirs();//创建文件夹
    }
    Scanner scan=null;//扫描输入
    try {
    scan=new Scanner(new FileInputStream(file));//实例化Scanner

    while(scan.hasNext()){//循环读取
    this.txt.append(scan.next()+" ");//设置文本
    }
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    if(scan!=null){
    scan.close();//关闭Scanner
    }
    }
    }else{//使用Toast提示用户SD卡不存在
    Toast.makeText(this, "SD卡不村在", Toast.LENGTH_LONG).show();
    }

    }

    @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;
    }

    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    canvas---HTML5新特性
    flex布局之兼容
    前端Blob对二进制流数据的处理方式
    execCommand的复制
    express快速入门
    react+redux+webpack+git技术栈
    react开发
    gulp工程化工具
    python---django安装
    vue+webpack+element-ui+git
  • 原文地址:https://www.cnblogs.com/liyuanjinglyj/p/4656609.html
Copyright © 2011-2022 走看看