win8.1 文件读写(一)

在windows store app 开发过程中,经常需要进行文件的读写操作,下面给出的是一些简单的文件读写,仅供参考


1.简单的文件写入操作

private async void write()

{

    StorageFolder storagefolder = ApplicationData.Current.LocalFolder;//获取文件夹路径

    StorageFile storagefile = null;

    storagefile = await storagefolder .CreateFileAsync(“Me.txt”, CreationCollisionOption.ReplaceExisting);//在指定路径下创建文件(可重写)

    if (storagefile !=null)

    {

        using (Stream stream = await storagefile.OpenStreamForWriteAsync())

        {

            byte[] content= Encoding.UTF8.GetBytes(“Hello World !”);

            await stream.WriteAsync(content, 0, content.Length);//数据写入

        }

    }

    else

    {

        //文件没找到

    }

}

这样,到程序的工作目录下,就可以看到一个名为Me.txt的文本文件,打开文件,明晃晃的几个大字:Hello World !

我们既然已经可以将数据写入到文件中,那么又如何读取文件中的内容呢,我们继续文件的读取操作

2.简单的文件读取操作

private async void read()

{

    StorageFolder storagefolder = ApplicationData.Current.LocalFolder;//获取文件夹路径

    StorageFile storagefile = null;

    storagefile = await storagefolder .GetFileAsync(“Me.txt”);

    if (storagefile != null)

    {

        IRandomAccessStreamWithContentType readstream = await storageFile.OpenReadAsync();

        using (Stream stream = readstream.AsStreamForRead((int)readstream.Size))

        {

            byte[] content = new byte[stream.Length];

            await stream.ReadAsync(content, 0, (int)stream.Length);

            string readcontent = Encoding.UTF8.GetString(content, 0, content.Length);//文件内容读取

        }

    }

    else

    {

    //未找到文件

    }

}

以上就是简单的文件读取,不要忘记

using Windows.Storage;

using Windows.Storage.Streams;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android 8.1上进行文件读写操作可以使用以下几种方法: 1. 使用File类进行文件操作: ```java File file = new File("/path/to/file"); // 检查文件是否存在 if (file.exists()) { // 读取文件内容 try { FileInputStream fis = new FileInputStream(file); // 使用fis进行文件读取操作 fis.close(); } catch (IOException e) { e.printStackTrace(); } // 写入文件内容 try { FileOutputStream fos = new FileOutputStream(file); // 使用fos进行文件写入操作 fos.close(); } catch (IOException e) { e.printStackTrace(); } } ``` 2. 使用Context类进行文件操作: ```java Context context = getApplicationContext(); String filename = "myfile"; // 读取文件内容 try { FileInputStream fis = context.openFileInput(filename); // 使用fis进行文件读取操作 fis.close(); } catch (IOException e) { e.printStackTrace(); } // 写入文件内容 try { FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE); // 使用fos进行文件写入操作 fos.close(); } catch (IOException e) { e.printStackTrace(); } ``` 3. 使用SharedPreferences类进行数据存储: ```java SharedPreferences preferences = getSharedPreferences("my_preferences", Context.MODE_PRIVATE); // 写入数据 SharedPreferences.Editor editor = preferences.edit(); editor.putString("key", "value"); editor.apply(); // 读取数据 String value = preferences.getString("key", ""); ``` 请注意,在Android 8.0及更高版本上,需要在Manifest文件中添加适当的权限。例如,如果要读写外部存储器上的文件,需要添加以下权限: ```xml <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ``` 同时,为了确保在Android 8.0及更高版本上正常运行,还需要运行时请求权限。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值