Android数据存取之file (文件存取操作)(3)

@Override
        public void onClick(View v) {
            Button button = (Button) v;
            String filename = filenameText.getText().toString();
            switch (button.getId()) {
            case R.id.save:
                String sendResult = "保存成功!";
                String content = contentText.getText().toString();
                OutputStream outputStream;
                try {
                    outputStream = FileSaveReadActivity.this.openFileOutput(filename, MODE_APPEND);
                    FileService.save(outputStream, content);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    sendResult = "保存失败";
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    sendResult = "保存失败";
                }
                Toast.makeText(FileSaveReadActivity.this, sendResult, Toast.LENGTH_LONG).show();
                break;

case R.id.read:
                try {
                        InputStream inputStream = FileSaveReadActivity.this.openFileInput(filename);
                        String contentShow = FileService.read(inputStream);
                        TextView contentShowLabel = (TextView) FileSaveReadActivity.this.findViewById(R.id.readContent);
                        contentShowLabel.setText(contentShow);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        Toast.makeText(FileSaveReadActivity.this, "读取失败", Toast.LENGTH_LONG).show();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        Toast.makeText(FileSaveReadActivity.this, "读取失败", Toast.LENGTH_LONG).show();
                    }
                break;
            }  
        }
    };
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwsggy.html