Android Studio-txt文件-内部文件计数

时间:2019-01-18 22:03:33

标签: java android android-studio

我是Android Studio编程的初学者。我想制作如下所述的apk:

  • 按下按钮
  • 整数1保存到txt文件
  • 如果我关闭apk并重新打开并再次按下按钮,我将得到2号到txt文件 并重复此过程(3,4,5 ... ect)。

我需要做的(在程序中):

  1. 创建文件(如果不存在)
  2. 查看是否为txt。文件内部已经有一些数字,该数字为+1,如果不存在(txt文件为空),我需要向txt添加整数1。文件。

    下面是我的程序,我遇到了 public void length() 问题-当我用log.d测试程序时总是显示空文件,但是我被检查了文本文件。设备文件浏览器中的文件,并且txt.file中的数字为1。我也尝试使用BufferdReader(br.readLine())并获得相同的结果。我不知道问题出在哪里...

public class MainActivity extends AppCompatActivity {

    Button btn;
    private static int Counter = 0;
    private static final String FILE_NAME = "data.txt";
    private static String TAG="MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.button);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Counter++;
                Log.d(TAG, "onClick: counter "+Counter);
                saveChec();
                Lengt();
                //load();
                // subSu();
            }
        });
    }

    private void subSu() {

    }

    public void Lengt(){
        long length = new File(getFilesDir().getAbsolutePath() + FILE_NAME).length();
        if (length == 0){
            //get alvays empty..
            Log.d(TAG, "Buff: empty ");
            save(); //add value 1 to txt file
            
        }else{
            Log.d(TAG, "Buff: full");
        }
    }

    public void saveChec() {

        FileOutputStream fos = null;

        try {
            fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
            Log.d(TAG, "saveChec: crated file ");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // return;

    }

    public void load() {

        FileInputStream fis = null;

        try {
            fis = openFileInput(FILE_NAME);
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
           //get alvays null
            if (br.readLine() == null) {
                Log.d(TAG, "load: empty file ");
                fis.close();
                save();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void save() {

        FileOutputStream fos = null;

        try {
            fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
            int number = 1;
            String numberAsString = Integer.toString(number);
             fos.write(numberAsString.getBytes());
            Log.d(TAG, "save: numberAsString "+numberAsString);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
       // return;
    }
}

谢谢您的回答!

2 个答案:

答案 0 :(得分:0)

改为使用此代码:-

public void SaveFile()
{
    try {
            //Getting Internal Storage
            File file = new File(Environment.getExternalStorageDirectory(),"");
            File dfile = new File(file+"/data.txt");
            FileWriter write = null;
            //if File Does Exists
            if (!dfile.exists()) {
                write = new FileWriter(file+"/data.txt");
                write.write("1");
                write.close();
            }
            else{
                FileReader read = new FileReader(dfile);
                BufferedReader br = new BufferedReader(read);
                String snum = br.readLine();
                if(snum!=null) {
                    //Converting Sring to Integer
                    int num = Integer.parseInt(snum);
                    num++;
                    write = new FileWriter(file+"/data.txt");
                    write.write(String.valueOf(num));
                    write.close();
                }
                else
                {                    
                    write = new FileWriter(file+"/data.txt");
                    write.write("1");
                    write.close();
                }
            }
        }catch (IOException ex){}
}

将这些权限添加到清单文件:-

<uses-permission android:name="READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="WRITE_EXTERNAL_STORAGE"></uses-permission>

答案 1 :(得分:0)

您必须使用文件吗?我将使用Shared Preferences来存储此值,因为它只是原始值。