生成TXT文档到U盘中

package navigation.chiphd.com.serialnodemo;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    String filecontent;
    private  String str1;
    private  String str2;
    String model;
    File writefile;
    private  EditText et;

    private  File localFile1;

    String []propertys = {"ro.boot.serialno", "ro.serialno"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView serialno = (TextView) findViewById(R.id.serialno);
        Button bt = (Button) findViewById(R.id.bt);
        Button bt1 = (Button) findViewById(R.id.bt1);
        et = (EditText) findViewById(R.id.et);



        for (String key : propertys){

            str1 = getAndroidOsSystemProperties(key);
            Log.e("", "get " + key + " : " + str1);

            serialno.setText("序列号" + str1);

        }

        bt.setOnClickListener(this);
        bt1.setOnClickListener(this);

    }

    static Method systemProperties_get = null;

    static String getAndroidOsSystemProperties(String key) {
        String ret;
        try {
            systemProperties_get = Class.forName("android.os.SystemProperties").getMethod("get", String.class);
            if ((ret = (String) systemProperties_get.invoke(null, key)) != null)
                return ret;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return "";
    }


    public void writefile(String paramString)
    {
        this.model = Build.MODEL;
        localFile1 = new File("/storage/usbcard1", this.model + "_SerialnoNum.text");
        try
        {
            this.filecontent = paramString;
            this.writefile = localFile1;
            if (!this.writefile.exists())
            {
                this.writefile.createNewFile();
                this.writefile = localFile1;
            }
            FileWriter localFileWriter = new FileWriter(this.writefile, true);
            localFileWriter.write(this.filecontent);
            localFileWriter.close();
            localFileWriter.flush();

            return;
        }
        catch (Exception localException)
        {
            System.out.println(localException.getMessage());
        }
    }


    public  void readTxtFile(){
        try {
            String encoding="GBK";
            if(localFile1.isFile() && localFile1.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(localFile1),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine()) != null){
                    System.out.println(lineTxt);

                   if (lineTxt.contains(str1)){

                       Toast.makeText(MainActivity.this, "机器号:"+str2 +":"+str1+ "文件写入成功", Toast.LENGTH_SHORT).show();

                   }else {

                       Toast.makeText(MainActivity.this, "文件还未写入", Toast.LENGTH_SHORT).show();

                   }

                }
                read.close();
            }else{
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            Toast.makeText(MainActivity.this, "读取文件内容出错", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }

    }


    @Override
    public void onClick(View v) {

        File localFile = new File("/storage/usbcard1");



        switch (v.getId()){

            case  R.id.bt  :

                if (localFile.exists()&&localFile.isDirectory()){

                    if (localFile.list()!=null){

                        str2 = et.getText().toString();

                        if (str2.length() != 0) {

                            writefile(str2 + "\t" + str1 + "\n");

                            try {
                                Thread.sleep(5000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                            Toast.makeText(MainActivity.this, "已写入,请检测", Toast.LENGTH_SHORT).show();

                        }else {

                            Toast.makeText(MainActivity.this, "存储失败,请检查", Toast.LENGTH_SHORT).show();
                        }
                    }

                }else {

                    Toast.makeText(MainActivity.this, "U盘路径有问题", Toast.LENGTH_SHORT).show();
                }




               break;
            case R.id.bt1:


                readTxtFile();


                break;




        }



    }

    }









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值