Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class ficheros{
- public static void main(String[] args) throws IOException{ //evitamos que nos salten excepciones, normalmente usariamos try-catch para capturarlas
- File path=new File("trabajo");
- System.out.println(path.getAbsolutePath());
- File f=new File(path, "prueba.txt");
- System.out.println(f.getAbsolutePath());
- if(path.mkdir()){
- System.out.println("El directorio: \"" + path.getName() + "\" creado correctamente");
- if(f.createNewFile()){
- System.out.println("El fichero: \"" + f.getName() + "\" creado correctamente");
- }
- }
- if(f.exists()){
- System.out.println("El fichero: \"" + f.getName() + "\" existe");
- }
- String[] dir = path.list();
- System.out.print("Contenido del directorio: ");
- for(String nameFile : dir){
- System.out.println(nameFile);
- }
- PrintWriter output = new PrintWriter(f);
- for(int i = 0; i<10;i++){
- output.println("linea: " + i);
- }
- output.close(); //hay que cerrar el fichero para que la info este disponible
- System.out.println(f.length());
- FileReader input = new FileReader(f); //leer fichero f
- int c = input.read();
- while(c!=-1){ //leerlo todo byte a byte, -1 detecta fin de fichero
- System.out.print((char)c); //te lo da en ascci, le fices que sea char y lo pasa solo
- c=input.read();
- }
- input.close();
- FileWriter fw = new FileWriter(f, true); //fichero donde escribimos y si true añade, si false trunca
- output=new PrintWriter(fw);
- for(int i = 10; i<20;i++){
- output.println("linea: " + i);
- }
- output.close();
- System.out.println(f.length());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement