Skip to content

Instantly share code, notes, and snippets.

@engividal
Created October 3, 2018 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save engividal/2f67e11616cb2def4c26a623d6bf15e8 to your computer and use it in GitHub Desktop.
Save engividal/2f67e11616cb2def4c26a623d6bf15e8 to your computer and use it in GitHub Desktop.
Funções para salvar arquivos no android
// Função para leitura do Arquivo
private void read(FileInputStream fis) throws IOException {
BufferedReader reader = new BufferedReader(
new InputStreamReader(fis));
StringBuilder sb = new StringBuilder();
String linha;
while ((linha = reader.readLine()) != null) {
if (sb.length() != 0) sb.append('\n');
sb.append(linha);
}
reader.close();
fis.close();
Log.i("Save", sb.toString());
}
// Função para escrita do arquivo
private void write(FileOutputStream fos) throws IOException {
PrintWriter writter = new PrintWriter(fos);
writter.println("isso é um teste");
writter.flush();
writter.close();
fos.close();
}
private void save(){
try {
FileOutputStream fos = openFileOutput("sensor.txt", Context.MODE_PRIVATE);
write(fos);
Log.i("Save", "Salvou Internamente");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void load(){
try {
FileInputStream fis = openFileInput("sensor.txt");
read(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment