Skip to content

Instantly share code, notes, and snippets.

@kakobotasso
Created September 10, 2012 17:28
Show Gist options
  • Save kakobotasso/3692325 to your computer and use it in GitHub Desktop.
Save kakobotasso/3692325 to your computer and use it in GitHub Desktop.
Gerando e lendo arquivo txt com java
package javaapplication1;
import java.io.*;
public class JavaApplication1 {
public static void main(String[] args) {
try {
// Conteudo
String content = "Teste";
// Cria arquivo
File file = new File("teste.txt");
// Se o arquivo nao existir, ele gera
if (!file.exists()) {
file.createNewFile();
}
// Prepara para escrever no arquivo
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
// Escreve e fecha arquivo
bw.write(content);
bw.close();
// Le o arquivo
FileReader ler = new FileReader("teste.txt");
BufferedReader reader = new BufferedReader(ler);
String linha;
while( (linha = reader.readLine()) != null ){
System.out.println(linha);
}
// Imprime confirmacao
System.out.println("Feito =D");
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Luan4560
Copy link

Nossa,ajudou bastante em um projeto pra faculdade. Valeu demais!

@gabbilopez
Copy link

amazing

@jonattasts
Copy link

Incredible !!

@MarcosPaes01
Copy link

´Perfeito, valeuu

@Lauro-Santos
Copy link

como faço pra editar o conteudo de uma arquivo?

@wemiliano
Copy link

Bem legal cara, gostaria de saber onde que esse arquivo foi salvo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment