elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Guía rápida para descarga de herramientas gratuitas de seguridad y desinfección


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Añadir numero
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Añadir numero  (Leído 1,910 veces)
mude

Desconectado Desconectado

Mensajes: 2


Ver Perfil
Añadir numero
« en: 1 Marzo 2018, 09:27 am »

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.
Código:
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


En línea

rub'n


Desconectado Desconectado

Mensajes: 1.217


(e -> λ("live now")); tatuar -> λ("α");


Ver Perfil WWW
Re: Añadir numero
« Respuesta #1 en: 1 Marzo 2018, 10:43 am »


Hola,

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


En línea



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 ki
mude

Desconectado Desconectado

Mensajes: 2


Ver Perfil
Re: Añadir numero
« Respuesta #2 en: 1 Marzo 2018, 16:59 pm »

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
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines