Añadir numero

Iniciado por mude, 1 Marzo 2018, 09:27 AM

0 Miembros y 1 Visitante están viendo este tema.

mude

Buenos días mi caso es el siguiente, estoy trabajando con archivos, y lo que hago es pasando una url me descargo el contenido a un archivo txt.
Pero ahora necesito que cada vez que le pase una nueva url, está se copie esa dirección a otro txt y las vaya numerando.Me he quedado atascado ahí.No consigo hacerlo.
package readywrite;
import java.io.*;
import java.net.*;
public class miclase {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

//Conexión a la web
String web = "http://www.aizea.es";

FileWriter fichero=null;
try {

URL url1 = new URL(web);
URLConnection conn = url1.openConnection();

// open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(
                           new InputStreamReader(conn.getInputStream()));

String inputLine;

//save to this filename
String fileName = "descarga.txt";
File file = new File(fileName);

if (!file.exists()) {
file.createNewFile();
System.out.println("El archivo "+fileName + ", Creado en el directorio");
}else {
System.out.println(fileName + ", ya existe en el directorio");
}


//use FileWriter to write file
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);

while ((inputLine = br.readLine()) != null) {
bw.write(inputLine);
}

bw.close();
br.close();

System.out.println("Hecho");


}
    finally {
    if (null!=fichero) fichero.close();
    }




//mensaje a escribir

//Escritura del fichero
try {
//Escritura del fichero
FileWriter fw = new FileWriter("1.txt", true);
BufferedWriter out = new BufferedWriter(fw);
PrintWriter wr = new PrintWriter(out);
int i = 0;
wr.append(i+": "+web+"\n");
wr.close();
fw.close();


}catch (Exception e) {
System.out.println("Error"+e);
}
try {
FileReader fr = new FileReader("1.txt");
//Necesito almacenar el texto A UN BUFFER
BufferedReader bf = new BufferedReader(fr);
System.out.println(bf.readLine());

}catch(Exception e) {
System.out.println("Error"+ e);
}
}

}

Gracias

rub'n


Hola,

Archivos de acceso aleatorio para eso, es porque se te esta reesbribiendo el fichero


rubn0x52.com KNOWLEDGE  SHOULD BE FREE!!!
If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen

mude

Me refiero añado una linea y sea 1 : linea1.
Paso un segunda línea en otro momento (otro día) y el archivo contenga:
1: linea1
2: linea2