0no

Ordenação, Leitura e Gravação - Gravação

0no
Nov 7th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. /*
  2. * Grave uma sequência de 500 números aleatórios em um arquivo txt, leia esse arquivo txt e faça a ordenação desses
  3. * números, utilize qualquer método de ordenação que quiser, grave em um novo txt o resultado.
  4. */
  5. import java.io.BufferedReader;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileReader;
  8. import java.io.FileWriter;
  9. import java.io.IOException;
  10. import java.io.PrintWriter;
  11.  
  12. public class Gravar {
  13.  
  14.     private static BufferedReader lerArq;
  15.     private static int maxNumeros = 7;
  16.     private static int[] numeros = new int[maxNumeros];
  17.    
  18.     public static void main(String[] args) throws IOException {
  19.         gravar(numerosAleatorios(), "num_des.txt");
  20.         int[] numeros = ler("num_des.txt");
  21.         int[] ordenados = ordenacao(numeros);
  22.         gravar(ordenados, "num_ord.txt");
  23.     }
  24.    
  25.     public static int[] numerosAleatorios(){
  26.         int[] numeros = {10,20,1,2,5,10,58};
  27.         return numeros;
  28.     }
  29.    
  30.     public static int[] ler(String nome) throws IOException{
  31.         FileReader arq;
  32.         try {
  33.             arq = new FileReader(nome);
  34.             lerArq = new BufferedReader(arq);
  35.             int i = 0;
  36.             String arqu = lerArq.readLine();
  37.             while (arqu != null) {
  38.                 numeros[i] = Integer.parseInt(arqu);
  39.                 i++;
  40.                 arqu = lerArq.readLine();
  41.             }
  42.             return numeros;
  43.         } catch (FileNotFoundException e) {
  44.             System.out.printf(e.getMessage());
  45.         }
  46.         return null;
  47.     }
  48.    
  49.     public static void gravar(int numeros[], String nome){
  50.         for(int i = 0; i < (numeros.length); i++){
  51.                try{
  52.                     FileWriter arq;
  53.                     if(i == 0){
  54.                         arq = new FileWriter(nome);
  55.                     }else{
  56.                         arq = new FileWriter(nome, true);
  57.                     }
  58.                     PrintWriter gravarArq = new PrintWriter(arq);
  59.                     if(i == maxNumeros - 1){
  60.                         gravarArq.printf(Integer.toString(numeros[i]));
  61.                     }else{
  62.                         gravarArq.printf(numeros[i] + "\n");
  63.                     }
  64.                     arq.close();
  65.                     }catch(IOException e){
  66.                         System.out.printf(e.getMessage());
  67.                     }
  68.         }
  69.     }
  70.    
  71.     public static int[] ordenacao(int v[]){
  72.         for (int i = v.length; i >= 1; i--) {
  73.             for (int j = 1; j < i; j++) {
  74.                 if (v[j - 1] > v[j]) {
  75.                     int aux = v[j];
  76.                     v[j] = v[j - 1];
  77.                     v[j - 1] = aux;
  78.                 }
  79.             }
  80.         }
  81.         return v;
  82.     }  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment