Simple issue with Android file output

I’m kind of stuck in something that should just be simple. I am trying to write several variables to a file, each on its own line, so that I can use readLine in another method to read the variables.

Here is the code:

package com.example.files2;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;

public class files2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String string1 = "Hey you";
    String string2 = "Is there anybody there";
    String string3 = "Can you hear me";

    setContentView(R.layout.main);

    try{

    File file = new File(Environment.getExternalStorageDirectory(), "file.txt");


    BufferedWriter writer = new BufferedWriter(new FileWriter(file));
    writer.write(string1);
    writer.newLine();
    writer.write(string2);
    writer.newLine();
    writer.write(string3);
    writer.newLine();
    writer.flush();
    writer.close();

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

}}

The file writes OK, but instead of each variable on its own line, I get only one line, which says: "You have someone there, you hear me."

So, I think .newLine () is not working, but why?

Is there a simple answer to this problem or a better way to achieve it?

Thanks to everyone, comments are warmly appreciated.

Joe

+3
source share
2 answers

, ? Windows? , Android, UNIX, \n \r\n, Windows. , adb shell, cat /sdcard/file.txt?

+7

- - UNIX/DOS ( , Android- Linux, [ ] Windows PC), - Vim (. http://www.vim.org/download.php) , linefeed ( Windows/DOS).

Android , " ", .

, - , .

+1

Source: https://habr.com/ru/post/1740823/


All Articles