Java程序无法读取特定的代码行

时间:2019-03-15 21:13:02

标签: java android

这是供参考的总体代码:

 package com.example.scoutingapp2019v9;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import android.support.v7.app.AppCompatActivity;

import android.view.View.OnClickListener;

import static android.provider.AlarmClock.EXTRA_MESSAGE;

public class EndgameEntryActivity extends AppCompatActivity {
    RadioButton end_level1;
    RadioButton end_level2;
    RadioButton end_level3;
    String[] data;
    String fileName;
    String path;
    String[] labels;
    Button savedata;
    EditText comments;

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


        data = new String[14];
//        Intent intent = getIntent();
//        String[] autoEntryData = intent.getStringArrayExtra(EXTRA_MESSAGE);
//        for (int i = 0; i < 12; i++) {
//            data[i] = autoEntryData[i];
//        }
        labels = new String[]{"Team Number", "Event", "Match Number", "Alliance", "Start Position", "Exited HabZone", "Number of HatchPanels Placed in Sandstorm", "Number of Cargo Placed in Sandstorm", "Teleop Number of HatchPanels Placed in Rockets", "Teleop Number of Cargo Placed in Rockets", "Teleop Number of HatchPanels Placed in CargoShip", "Teleop Number of Cargo Placed in CargoShip", "End Position", "Comments"};
        savedata = (Button) findViewById(R.id.savedata);
        final RadioButton rb9 = (RadioButton) findViewById(R.id.end_level1);
        final RadioButton rb10 = (RadioButton) findViewById(R.id.end_level2);
        final RadioButton rb11 = (RadioButton) findViewById(R.id.end_level3);
        final RadioButton rb12 = (RadioButton) findViewById(R.id.end_level_0);
        comments = (EditText) findViewById(R.id.comments);
        savedata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (rb9.isSelected()) {
                    data[12] = ("Level 1");
                } else if (rb10.isSelected()) {
                    data[12] = ("Level 2");
                } else if (rb11.isSelected()) {
                    data[12] = ("Level 3");
                } else if (rb12.isSelected()) {
                    data[12] = ("DNF");
                }
                data[13] = comments.getText().toString();
                saveData(data, labels);
            }
        });
    }
    private void saveData(String[] saveData, String[] saveLabels){
        path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Documents/TeamData2019/";//temporarily without subfolder
        fileName = data[0] + "_" + data[1] + "_" + data[2] + ".json";
        try{
            new File(path).mkdir();
            path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Documents/TeamData2019/" + data[1] + '/';
            new File(path).mkdir();
            File file = new File(path + fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            else{
                return;
            }
            JSONObject obj = new JSONObject();
            for(int i = 0; i < saveData.length; i++){
                obj.put(saveLabels[i],saveData[i]);
        }

        FileWriter fileWriter = new FileWriter(file,true);
        fileWriter.write(obj.toString());
        fileWriter.flush();

        Toast.makeText(getApplicationContext(),"Data saved", Toast.LENGTH_LONG).show();
    }catch(Exception e){
        e.printStackTrace();
    }
}
}

我对这些行有特殊的疑问:

try{
                new File(path).mkdir();
                path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Documents/TeamData2019/" + data[1] + '/';
                new File(path).mkdir();
                File file = new File(path + fileName);
                if (!file.exists()) {
                    file.createNewFile();
                }
                else{
                    return;
                }
                JSONObject obj = new JSONObject();
                for(int i = 0; i < saveData.length; i++){
                    obj.put(saveLabels[i],saveData[i]);
            }

由于我想做的事,程序不会读取这些行并跳过它们,而android studio本身说这些行的结果将被忽略(并且我使用方法断点确认了这一点)。我尝试过更改代码中的括号,但没有任何效果。因为我仍然是这种语言的初学者,所以这可能对我来说是一个忽视。 有人对出什么问题有任何想法吗?

1 个答案:

答案 0 :(得分:0)

答案是文件已经创建,正如用户hfarhanahmed指出的那样,我删除了程序创建的前一个文件,程序再次开始工作。