• Android 学习笔记 文本文件的读写操作


    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    
        <Button 
            android:id="@+id/btn01"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="保存" />
        <Button
            android:id="@+id/btn02"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="读取" />
        
        <TextView
            android:id="@+id/msg"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    MainActivity.java

    public class MainActivity extends Activity {
        private static final String FILENAME="Test01.txt";
        private Button btn01=null;
        private Button btn02=null;
        private TextView msg=null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            super.setContentView(R.layout.activity_main);
            this.btn01=(Button)super.findViewById(R.id.btn01);
            this.btn02=(Button)super.findViewById(R.id.btn02);
            this.btn01.setOnClickListener(new OnClickListenerImpl());
            this.btn02.setOnClickListener(new OnClickListenerImpl());
            this.msg=(TextView)super.findViewById(R.id.msg);
        }
        private class OnClickListenerImpl implements OnClickListener{
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switch (v.getId()) {
                case R.id.btn01:
                    FileOutputStream output=null;
                    try {
                        output=MainActivity.this.openFileOutput(FILENAME, Activity.MODE_PRIVATE);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    PrintStream out=new PrintStream(output);
                    out.println(sdf.format(new Date()));
                    out.println("文本内容aa");
                    out.close();
                    Toast.makeText(MainActivity.this, "文件已经保存", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.btn02:
                    FileInputStream input=null;
                    try {
                        input=MainActivity.this.openFileInput(FILENAME);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    Scanner scanner=new Scanner(input);
                    MainActivity.this.msg.setText("");
                    MainActivity.this.msg.append("-------Start--------
    ");
                    while (scanner.hasNext()) {
                        MainActivity.this.msg.append(scanner.next()+"
    ");
                    }
                    MainActivity.this.msg.append("-------End--------
    ");
                    Toast.makeText(MainActivity.this, "文件已经读取", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
                }
            }
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }

     如果需要操作SD卡上的文件,需要在AndroidManifest.xml中添加权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  • 相关阅读:
    Jmeter测试接口详细步骤(三)跨线程组传值-BeanShell 后置处理程序
    Jmeter测试接口详细步骤(二)HTTP Cookie管理器
    Jmeter测试接口详细步骤(一)基础操作
    最优化学习3
    最优化学习2
    最优化学习---从解方程到最优化1
    最优化学习---从解方程到最优化
    博客园 文章和随笔区别 (转
    refinedet tensorRT实现
    crnn pytorch 训练、测试
  • 原文地址:https://www.cnblogs.com/taobox/p/3375480.html
Copyright © 2020-2023  润新知