如何编写/读取多行.txt文件?

时间:2019-05-31 18:13:22

标签: android file

我想将数据保存在文本文件中。在另一个活动中,我希望能够读取保存的数据。我想每次保存multible值并将它们放到一行上,下一个值需要放在新行中。我尝试\n System.getProperty("line.separator"); System.lineSeparator();\n\r换行,但这似乎不起作用。

我使用以下代码写入文件:

Context context = getApplicationContext();
writedatatofile(context);

protected void writedatatofile(Context context){
        try
        {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("data_log.txt", Context.MODE_APPEND));
            String data;
            if (newstart){
                data = "Exersice started \n" + "t.s a.s t.a a.a cnst";
            } else {
                data = (Integer.toString(time_step)+Integer.toString(new_average_step)+Integer.toString(time_footaid)+Integer.toString(new_average_aid)+Boolean.toString(rhythmconsistent)+"\n");
            }
            outputStreamWriter.append(data);
            outputStreamWriter.close();
            Toast.makeText(this, "Data has been written to File", Toast.LENGTH_SHORT).show();
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }

和这段代码读取文件:

Context context = getApplicationContext();
String fileData = readFromFile(context, fileName);
TextView datalog = findViewById(R.id.datalog);
datalog.setText(fileData);

private String readFromFile(Context context, String fileName){
        String ret = " ";
        try {
            InputStream inputStream = context.openFileInput(fileName);
            if (inputStream != null) {
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String receiveString = "";
                StringBuilder stringBuilder = new StringBuilder();
                while ((receiveString = bufferedReader.readLine()) != null) {
                    Toast.makeText(this, "Data received", Toast.LENGTH_SHORT).show();
                    stringBuilder.append(receiveString);
                }
                inputStream.close();
                ret = stringBuilder.toString();
            } else {
                Toast.makeText(this, "No data received", Toast.LENGTH_SHORT).show();
            }
        } catch (FileNotFoundException e){
            Toast.makeText(this, "File not found", Toast.LENGTH_SHORT).show();
        } catch (IOException e){
            Toast.makeText(this, "Can not read file", Toast.LENGTH_SHORT).show();
        }
        return ret;
    }

但是没有多行,而是彼此紧接。有人知道如何解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

编写文件时:

BufferedWriter writer = new BufferedWriter(outputStreamWriter);
writer.write(data);
writer.newLine(); // <-- this is the magic
writer.close();

答案 1 :(得分:0)

您可以使用以下代码从.txt文件中读取数据。

  File exportDir = new File(Environment.getExternalStorageDirectory(), File.separator + "bluetooth/abc.txt");

        if (exportDir.exists()) {
            FileReader file = null;
            try {
                file = new FileReader(exportDir);

                BufferedReader buffer = new BufferedReader(file);
                String line = "";
                int iteration = 0;
                while ((line = buffer.readLine()) != null) { //read next line
                    if (iteration == 0) { //Skip the 1st position(header)
                        iteration++;
                        continue;
                    }
                    StringBuilder sb = new StringBuilder();
                    String[] str = line.split(",");

                    //get the single data from column
                   String text1 = str[1].replace("\"", "");
                   String text2 = str[2].replace("\"", "");



                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

相关问题