Android文件的读写操作

文件的基本操作:

    创建一个java项目:

        项目中创建一个类:

        public class FileUtil{

            public static final String FILE_NAME = "myfile.txt";

            public static final String FOLDER_NAME = "NewFolder"

                        + File.separator + "Subfolder";

        }

 

        再创建一个主类:

        public static void main(String[] args){

            createTxt();

 

        }

 

        public void createTxt(){

            File file = new File(FileUtil.FILE_NAME);

            if(!file.exists()){

                try{

                    file.createNewFile();

                    System.out.println("文件已经创建了");

                }catch(IOException e){

                    e.printStackTrace();

                }

            }else{

                System.out.println("文件已经存在");

                System.out.println("文件名为:"+ file.getName());

                System.out.println("文件绝对路径为:"+ file.getAbsolutePath());

                System.out.println("文件的相对路径为:"+ file.getPath());

 

                //在文件中数据内容之后

                System.out.println("文件大小为:"+ file.length()+"字节");

                System.out.println("文件是否可读:"+ file.canRead());

                System.out.println("文件是否可写:"+ file.canWrite());

                System.out.println("文件是否隐藏:"+ file.isHidden());               

            }

        }

 

        public void deleteFile(){

            File file = new File(FileUtil.FILE_NAME);

            if(file.exists()){               

                    file.delete();

                    System.out.println("文件已经被删除了");

               }

        }

 

        public void rename(){

            File file = new File(FileUtil.FILE_NAME);

            File newfile = new File("anotherFile.txt");

            file.renameTo(newfile);

             System.out.println("文件已经成功重命名了"+ file.getName()); 

        }

 

        //文件夹的操作

        public void createFolder(){

            File folder = new File(FileUtil.FOLDER_NAME);

            if(!folder.exists()){

                folder.mkdirs();

            }

        }

 

        public void removeFolder(){

            File folder = new File(FileUtil.FOLDER_NAME);

            if(folder.exists()){

                folder.delete();//只能移除下一层目录,并且目录是空的

            }

        }

 

 

创建Android项目:

       

读取Assets中的文件数据:

activity_main.xml:

    <Button

        android:id="@+id/readtxtBtn"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="读取TXT数据"/>

 

在Assets文件夹中创建info.txt文件,随便输入几行文本。

 

MainActivity.java:

 

    private static final String TAG = "ReadAssets";

    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        findViewById(R.id.readtxtBtn).setOnClickListener(new View.OnClickListener){

            public void onCreate(View arg0){

                try{

                    InputStream is = getResources().getAssets().open("info.txt");

                    InputStreamReader isr = new InputStreamReader(is, "UTF-8");

                    BufferedReader bfr = new BufferedReader(isr);

                    String str = "";

                    while((str = bfr.readLine()) != null){

                        Log.i(TAG, str);

                    }

                }catch(IOException e){

                    e.printStackTrace();

                }

            }

        }

    }

 

 

 

读取raw目录中的文件数据:

    activity_main文件:

        <Button

                android:layout_width="fill_parent"

                android:layout_height="wrap_content"

                android:text="读取RAW文件夹中的数据"

                android:onClick="click"/>

 

    在res目录中床架一个raw文件夹,并在里面创建一个文件info.txt,并在文件夹中随便输入几行数据

 

    MainActivity文件中:

        private static final String TAG = "RawData";

        public void click(View v){

            try{

                InputStream is = getResources().openRawResource(R.raw.info);

                InputStreamReader isr = new InputStreamReader(is, "UTF-8");

                BufferedReader bfr = new BufferedReader(isr);

                String inString = "";

                while((inString = bfr.readLine()) != null){

                    Log.i(TAG, inString);

                }

            }catch(Exception e){

                e.printStackTrace();

            }

        }

 

 

 

读写内部存储的文件数据:

    内部存储空间在DDMS中的FileExplorer的data/data/files/XXX

    在activity.xml中:

        <EditText

            android:id="@+id/et"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:hint="在这里输入内容"/>

        <Button

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="保存数据"

            android:onClick="clickw"/>

        <Button

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="读取数据"

            android:onClick="clickr"/>

        <TextView

            android:id="@+id/show"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"/>

 

 

    MainActivity文件中:

        private EditText et; //在ONCreate方法中声明

        private TextView show;

        private String fileName = "test";

 

        public void clickw(View view){

            try{

                FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);

                OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");

                osw.write(et.getText().toString());

                osw.flush();

                fos.flush();

                osw.close();

                fos.close();

                Toast.makeText(getApplicationContext(), "写入完成", 1).show();

            }catch(Exception e){

                e.printStackTrace();

            }           

        }

       

         public void clickr(View view){

            try{

                FileInputStream fis = openFileInput(fileName);

                InputStreamReader is = new InputStreamReader(fis, "UTF-8");

                char input[] = new char[fis.available()];

                is.read(input);

                is.close();

                fis.close();

                String readed = new String(input);

                show.setText(readed);

            }catch(Exception e){

                e.printStackTrace();

            }           

        }

 

 

 

 

读取外部存储(SD卡)的文件数据:

外部存储空间在DDMS中的FileExplorer的mnt/sdcard/XXX (真正的SDCARD在:storage/sdcard)

 

    在activity.xml中:

        <EditText

            android:id="@+id/editText"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:hint="在这里输入内容"/>

        <Button

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="写入数据"

            android:onClick="clickw"/>

        <Button

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="读取数据"

            android:onClick="clickr"/>

        <TextView

            android:id="@+id/show"

            android:text="这里显示读到的数据"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"/>

 

 

    MainActivity文件中:

        private EditText editText;//在ONCreate方法中实例化

        private TextView show;

 

        //写入数据

        public void clickw(View view){           

                File sdcard = Environment.getExternalStorageDirectory();//路径

                File myfile = new File(sdcard, "This is my file.txt");

                if(!sdcard.exists()){

                    Toast.makeText(getApplicationContext(), "当前系统不具备SD卡目录", 1).show();

                    return;

                }

            try{

                myfile.createNewFile();

                Toast.makeText(getApplicationContext(), "文件已创建完成", 1).show();

                FileOutputStream fos = new FileOutputStream(myfile);

                OutputStreamWriter osw = new OutputStreamWriter(fos);

                osw.write(et.getText().toString());

                osw.flush();

                osw.close();

                fos.close();

                Toast.makeText(getApplicationContext(), "文件已写入完成", 1).show();

            }catch(Exception e){

                e.printStackTrace();

            }           

        }

       

        //读取数据

         public void clickr(View view){

            File myfile = new File(sdcard, "This is my file.txt");

            if(myfile.exists()){

                try{

                    FileInputStream fis = new FileInputStream(myfile);

                    InputStreamReader isr = new InputStreamReader(fis, "UTF-8");

                    char[]  input = new char[fis.available()];

                    isr.read(input);

                    isr.close();

                    fis.close();

                    String inString = new String(input);

                    show.setText(inString);

                }catch(Exception e){

                    e.printStackTrace();

                }           

            }           

        }

 

        在清单文件中添加权限:

        android.permission.WRITE_EXTERNAL_STORAGE

 

posted @ 2015-11-04 18:33  飞牛冲天  阅读(386)  评论(0编辑  收藏  举报