Created
October 3, 2018 02:10
-
-
Save engividal/2f67e11616cb2def4c26a623d6bf15e8 to your computer and use it in GitHub Desktop.
Funções para salvar arquivos no android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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