How can I take a break when writing a file on Android?

4

I have been having problems with writing files on Android, I need to fill out a form and then store it in a PDF or txt file, for the moment I am trying it in txt but I can not write more than one line in it, due to that when trying to write on it and put line breaks at the end of each the content that is after the line break does not appear.

This is my writing code:

    public void escribirDatos(){
    try {
        OutputStreamWriter fout = new OutputStreamWriter(openFileOutput(textEdit.getText().toString(), Context.MODE_APPEND));

        fout.write("Fecha: 29/8/2017 Hora: 22:00" + '\n' + "Lugar y área: \t\t Levantamiento realizado por:");
        fout.close();
        Log.e("Creación","Archivo creado correctamente");

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

}

Reading:

public void leerDatos(){
    try {

        BufferedReader finLinea = new BufferedReader(new InputStreamReader(openFileInput(textEdit.getText().toString())));
        String texto = finLinea.readLine();
        textView.setText(texto);
        finLinea.close();
    }catch (Exception ex)
    {
        Log.e("Ficheros","Error al leer archivo");

    }

}

I hope you can help me and thank you very much for your attention.

    
asked by Jonathan Bustamante 05.01.2017 в 20:31
source

2 answers

5

Review the following Link which is the documentation of the OutputStreamWriter

but basically you can do something like this:

public static void writeFile1() throws IOException {
File fout = new File("out.txt");
FileOutputStream fos = new FileOutputStream(fout);

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

for (int i = 0; i < 10; i++) {
    bw.write("something");
    bw.newLine();
}


    bw.close();
}
    
answered by 05.01.2017 / 20:40
source
0

Simply your OutputStreamWriter adds a carriage return and new line:

osw.append("\r\n");

would be:

  try {
        OutputStreamWriter fout = new OutputStreamWriter(openFileOutput(textEdit.getText().toString(), Context.MODE_APPEND));

        fout.append("\r\n");  //Esto agregaría el salto de línea.

        fout.write("Fecha: 29/8/2017 Hora: 22:00" + '\n' + "Lugar y área: \t\t Levantamiento realizado por:");
        fout.close();
        Log.e("Creación","Archivo creado correctamente");

    }

Another option is to use the newLine() method of the class BufferedWriter

  

newLine () : A newLine () method is provided , who uses his own   notion of platform separator, defined by the system property    line.separator . Not all platforms   they use the new line character ('\ n') to end lines.   Calling this method to finish each line of exit is preferable   to write a new line character directly.

example:

    FileOutputStream fos = new FileOutputStream(fout); 
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

   bw.write("Fecha: 29/8/2017 Hora: 22:00" + '\n' + "Lugar y área: \t\t Levantamiento realizado por:"); 
   bw.newLine(); //Escribe nueva linea.
    
answered by 05.01.2017 в 20:54