Created
September 10, 2012 17:28
-
-
Save kakobotasso/3692325 to your computer and use it in GitHub Desktop.
Gerando e lendo arquivo txt com java
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
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(); | |
} | |
} | |
} |
amazing
Incredible !!
´Perfeito, valeuu
como faço pra editar o conteudo de uma arquivo?
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
Nossa,ajudou bastante em um projeto pra faculdade. Valeu demais!