Saving files in txt with Android Studio

0

I'm having trouble saving the user-entered name in a txt file.

When I press save, it says it was created, but I can not read it.

Context fileContext;

public BancoDeDados(Context fileContext) {
    this.fileContext = fileContext;
}

public void gravar(String nome) {
    try {
        FileOutputStream os = fileContext.getApplicationContext().openFileOutput("bdNome.txt", Context.MODE_PRIVATE);
        os.write(nome.getBytes());
        os.close();
        System.out.println("Salvou");
    } catch (IOException e) {
        System.out.println("Deu erro");
    }
}
    
asked by anonymous 07.07.2017 / 19:08

1 answer

1

Guys, here is the answer. I did it.

public void gravar ( String data ) {
        try {
            FileOutputStream fOut = fileContext.openFileOutput ( "arquivo.txt" , Context.MODE_PRIVATE ) ;
            OutputStreamWriter osw = new OutputStreamWriter ( fOut ) ;
            osw.write ( data ) ;
            osw.flush ( ) ;
            osw.close ( ) ;
            System.out.println("ok");
        } catch ( Exception e ) {
            e.printStackTrace ( ) ;
            System.out.println("Deu erro");
        }
    }

public String ler ( ) {
    StringBuffer datax = new StringBuffer("");
    try {
        FileInputStream fIn = fileContext.openFileInput ( "arquivo.txt" ) ;
        InputStreamReader isr = new InputStreamReader ( fIn ) ;
        BufferedReader buffreader = new BufferedReader ( isr ) ;

        String readString = buffreader.readLine ( ) ;
        while ( readString != null ) {
            datax.append(readString);
            readString = buffreader.readLine ( ) ;
        }

        isr.close ( ) ;
        System.out.println("ok");
    } catch ( IOException ioe ) {
        System.out.println("Deu erro");
        ioe.printStackTrace ( ) ;
    }
    return datax.toString() ;
}
    
07.07.2017 / 19:44