AndroidStudio-将用户信息保存为CSV文件并阅读

时间:2019-01-22 10:10:00

标签: java android file csv

我是Android Studio编程的新手。我正在尝试创建一个简单的应用程序以获得基本经验。在应用程序中,我需要将单个输入存储到内部存储器中的文件(最好是CSV)。首先,我试图保存用户数据-我的姓名和电话号码。保存方法如下:

public void save(View view)
    {
        String fileName = "user.csv";
        ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
        File directory = contextWrapper.getDir(getFilesDir().getName(), ContextWrapper.MODE_PRIVATE);
        File file = new File(directory, fileName);

        String data = "FirstName,LastName,PhoneNumber";
        FileOutputStream outputStream;
        try {
            outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
            outputStream.write(data.getBytes());
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

数据似乎已保存,我已重定向到MainActivity。方法如下:

protected void onCreate(Bundle savedInstanceState) {
    File file = new File(getFilesDir(),"user.csv");
    if(!file.exists()) {
        Intent intent = new Intent(this, activity_login.class);
        startActivity(intent);
    }
    else {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv_name = findViewById(R.id.tv_name);
        TextView tv_phone = findViewById(R.id.tv_phone);

        BufferedReader br = null;
        try {
            String sCurrentLine;
            br = new BufferedReader(new FileReader("user.csv"));
            while ((sCurrentLine = br.readLine()) != null) {
                tv_name.setText(sCurrentLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

TextView tv_name中没有存储值,该框为空。我在哪里犯错?

非常感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

以此来读取文件:

FileInputStream fis = new FileInputStream("your full file path");
BufferedReader bfr = new BufferedReader(new InputStreamReader(fis));

答案 1 :(得分:0)

下面是用于创建.csv文件并将数据插入其中的代码。

有关我的答案的更多信息:https://stackoverflow.com/a/48643905/8448886

此处编码:

var a1 = true && 'Cat' // no false values so returns true
var a2 = 'cat' && false // one false is there so returns false
var a3 = 'cat' && undefined // returns false
var a4 = 'cat' && null // returns false
var a5 = 'cat' && 1 // returns true
var a6 = 'false' && 'false' // returns true

and so on....

答案 2 :(得分:0)

要读取保存的数据,请使用此方法UTF_8编码

public static String readAsString(InputStream is) throws IOException {
    BufferedReader reader = null;
    StringBuilder sb = new StringBuilder();
    try {
        String line;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            reader = new BufferedReader(new InputStreamReader(is,UTF_8));
        }
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
    return sb.toString();
}

按以下方法使用此方法,分析文件“ user.csv”。

public static String readFileAsString(File file) throws IOException {
    return readAsString(new FileInputStream(file));
}

获取字符串并设置textView

相关问题