Java - como imprimir el resultado de mi programa de numeros primos en un txt

 
Vista:
Imágen de perfil de eduardo martinez

como imprimir el resultado de mi programa de numeros primos en un txt

Publicado por eduardo martinez (3 intervenciones) el 12/03/2016 06:08:04
hola que tal a todos,necesito de su ayuda por favor,necesito saber como imprimir el resultado de mi programa en un txt. Tambien comento que lo hice con scanner pero si alguien puede decirme como leer los valores valores desde un txt e imprimir el resultado en txt se lo agradeceria mucho,dejor el codigo
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public static boolean Primo (int num) {
 
        boolean Primo = true;
        int division = 2;
        while ((Primo)&& (division!=num))
            if (num%division==0){
                Primo = false;
            } else{
                division++;
                 }
       return Primo;
 
       }
 
    public static void main(String[] args)
    {
//        int i;
//        Scanner leer= new Scanner(System.in);
//        System.out.println("introduzca un numero menor a 50:");
//        i = leer.nextInt();
      boolean Primo;
        for(int i = 2; i<=50; i++){
            Primo = Primo(i);
            if(Primo==true){
                System.out.println(i+" Es Primo");
            }else{
                System.out.println(i+" No es Primo");
            }
        }
    }
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Jordi

como imprimir el resultado de mi programa de numeros primos en un txt

Publicado por Jordi (38 intervenciones) el 12/03/2016 07:39:17
Hola Eduardo,

Escribir en fichero

Lo más sencillo es utilizar la librería PrintWriter. Sólo hace falta lo siguiente:

1
2
3
4
PrintWriter writer = new PrintWriter("fichero.txt", "UTF-8"); // Inicializar
writer.println("linea1"); // Escribir
writer.println("linea2");
writer.close(); // Cerrar fichero

Leer de fichero

1
2
3
4
5
6
BufferedReader in = new BufferedReader(new FileReader("<Filename>")); // Inicializar
String line;
while((line = in.readLine()) != null) { // Leer línea
    System.out.println(line); // Tratar línea
}
in.close(); // Cerrar fichero

Resultado del código

Así quedaría tu código añadiendo ambas funcionalidades. Le he hecho algún que otro ajuste en la sintaxis. Por cierto, te faltaba cerrar una llave del while de la función Primo(int num).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public static boolean Primo (int num) {
	boolean Primo = true;
    int division = 2;
    while (Primo && division != num) {
		if (num%division == 0){
			Primo = false;
		}
		else {
			division++;
		}
	}
    return Primo;
}
 
public static void main(String[] args) {
	BufferedReader entrada = new BufferedReader(new FileReader("ficheroEntrada.txt"));
	PrintWriter salida = new PrintWriter("ficheroSalida.txt", "UTF-8");
	boolean Primo;
	String linea;
	while((linea = entrada.readLine()) != null) {
		if(Primo(Integer.parseInt(linea))) { // Cogemos el número escrito en la línea
			salida.println(linea);
		}
 
	}
	entrada.close();
	salida.close();
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar