Working with Files

What you learn: You will learn how to access file (read and write) and why we cannot use standard Java file-access.

Question Problems/Questions: Write them right below...

Difficulty: 1 of 5 Smile

What it will look like:

Laughing This is the first tutorial without a screenshot Laughing


Description:
To understand why we cannot use standard java file access, like:

Java:
FileWriter f = new FileWriter ( "impossible.txt" );
// throws: 'java.io.FileNotFoundException: /impossible.txt '


, we have to understand the Security-Model of Android.
Each *.apk File that is installed on the Emulator/Device gets its own User-ID from the Linux System. This ID is the key to the sandbox of the application. This 'sandbox' protects the application (and its files) from other bad Twisted Evil apps, that i.e. want to manipulate the files we created in a bad manner (Like writing into them: "Whos reads this is... dumb ! Hhahaha").

Note wrote:
But, writing to SD-Cards is still possible with 'normal' Java methods Exclamation
Java:
FileWriter f = new FileWriter ( "/sdcard/download/possible.txt" );

will work without any problem, you just need to add virtual sdcard to your emulator.



Every file that you generate with your code gets is 'signed' with the User-ID of the Application that created it. One From within your app you can set flags to make the file accessible (read and/or write) for other applications with other User-IDs (Flags: MODE_WORLD_READABLE and/or MODE_WORLD_WRITEABLE).

There is a way to make two applications use the same userid (look here).



1.Writing a file...

Java:
        try { // catches IOException below
                final String TESTSTRING = new String ( "Hello Android" );
               
                // ##### Write a file to the disk #####
                /* We have to use the openFileOutput()-method
                * the ActivityContext provides, to
                * protect your file from others and
                * This is done for security-reasons.
                * We chose MODE_WORLD_READABLE, because
                *  we have nothing to hide in our file */
        
                FileOutputStream fOut = openFileOutput ( "samplefile.txt",
                                        MODE_WORLD_READABLE );
                OutputStreamWriter osw = new OutputStreamWriter (fOut )

                // Write the string to the file
               osw. write (TESTSTRING );
                /* ensure that everything is
                * really written out and close */

               osw. flush ( );
               osw. close ( );


2.Reading the file back...

Java:
                // ##### Read the file back in #####
               
                /* We have to use the openFileInput()-method
                * the ActivityContext provides.
                * Again for security reasons with
                * openFileInput(...) */

                FileInputStream fIn = openFileInput ( "samplefile.txt" );
                InputStreamReader isr = new InputStreamReader (fIn );
                /* Prepare a char-Array that will
                * hold the chars we read back in. */

                char [ ] inputBuffer = new char [TESTSTRING. length ( ) ];
                // Fill the Buffer with data from the file
               isr. read (inputBuffer );
                // Transform the chars to a String
                String readString = new String (inputBuffer );
               
                // Check if we read back the same chars that we had written out
                boolean isTheSame = TESTSTRING. equals (readString );

                // WOHOO lets Celebrate =)
               Log. i ( "File Reading stuff", "success = " + isTheSame );

           } catch ( IOException ioe ) {
               ioe. printStackTrace ( );
           }



The file is now located in the following folder on your emulator:

" /data/data/your_project_package_structure/files/samplefile.txt"
How to browse the FileSystem of the Emulator watch 3 posts below.


Idea You delete a file with the following method coming from ApplicationContext:

Java:
public boolean deleteFile ( String name )
// Delete the given private file associated with this Context's application package.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值