网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Android数据存储(二)文件的使用-创新互联

在Android中,我们可以将一些数据直接以文件的形式保存在设备中。例如:一些文本文件、PDF文件、音视频文件和图片等。Android  提供了文件读写的方法。

成都创新互联公司成都企业网站建设服务,提供成都做网站、网站设计网站开发,网站定制,建网站,网站搭建,网站设计,自适应网站建设,网页设计师打造企业风格网站,提供周到的售前咨询和贴心的售后服务。欢迎咨询做网站需要多少钱:18980820575

    通过 Context.openFileInput()方法获得标准Java文件输入流(FileInputStream),通过Context.openFileOutput()方法获得标准Java文件输出流( FileOutputStream )。使用

Resources.openRawResource(R.raw.myDataFile)方法返回InputStream。

示例如下,新建一个Activity,添加两个TextView和两个Button,点击第一个Button,将TextView上的数据写到文件中,点击第二个Button,将文件中的数据写到TextView中。

MainActivity.java:

public class MainActivity extends Activity {
    private String filename = "file.txt";
    private TextView mytext1,mytext2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mytext1=(TextView)this.findViewById(R.id.text1);
        mytext2=(TextView)this.findViewById(R.id.text2);
        Button button1=(Button)this.findViewById(R.id.btn_read);
        Button button2=(Button)this.findViewById(R.id.btn_write);
        button1.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                mytext2.setText(read());
            }
        });
        button2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                write(mytext1.getText().toString());
            }
        });
    }
    protected String read(){
        try {
            FileInputStream fis = openFileInput(filename);
            try {
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                return new String(buffer);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    protected void write(String str) {
        try {
            FileOutputStream fos = openFileOutput(filename,MODE_APPEND);
            try {
                fos.write(str.getBytes());
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

activity_main.xml:



    
    
    
    
        

运行结果如下:        点击write将第一个TextView写入文件,点击read将数据读出到第二个TextView

                    Android 数据存储(二)  文件的使用                     Android 数据存储(二)  文件的使用

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前名称:Android数据存储(二)文件的使用-创新互联
浏览路径:http://bjjierui.cn/article/djoopg.html

其他资讯