Advertisement
jaVer404

level19.lesson05.task03_done but not tested

Feb 19th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. package com.javarush.test.level19.lesson05.task03;
  2.  
  3. /* Выделяем числа
  4. Считать с консоли 2 имени файла.
  5. Вывести во второй файл все числа, которые есть в первом файле.
  6. Числа выводить через пробел.
  7. Закрыть потоки. Не использовать try-with-resources
  8.  
  9. Пример тела файла:
  10. 12 text var2 14 8v 1
  11.  
  12. Результат:
  13. 12 14 1
  14. */
  15.  
  16. import java.io.*;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19.  
  20. public class Solution {
  21.     public static void main(String[] args)  {
  22.         BufferedReader reader = null;
  23.        
  24.         try
  25.         {
  26.             reader = new BufferedReader(new InputStreamReader(System.in));
  27.             String fileName = reader.readLine();
  28.             String outFile = reader.readLine();
  29.             reader.close();
  30.             writeToFile(outFile,getNums(fileName));
  31.             reader.close();
  32.         }
  33.         catch (Exception e) {}
  34.         finally
  35.         {
  36.             if (reader!=null) {
  37.                 try
  38.                 {
  39.                     reader.close();
  40.                 }
  41.                 catch (IOException e) {}
  42.             }
  43.         }
  44.     }
  45.  
  46.     public static ArrayList <String> getNums (String sourceFile){
  47.         ArrayList<String>myNums = new ArrayList<String>();
  48.         BufferedReader readeFile = null;
  49.         try
  50.         {
  51.             readeFile = new BufferedReader(new FileReader(sourceFile));
  52.             String tmp="";
  53.             while ((tmp=readeFile.readLine())!=null){
  54.                 //tmp=tmp.replaceAll("[^0-9\\s]", " ");
  55.                 String[]strings = tmp.split(" ");
  56.                 for (String s : strings){
  57.                     if (isNumeric(s)) {
  58.                         myNums.add(s);
  59.                     }
  60.                 }
  61.             }
  62.             readeFile.close();
  63.             myNums.removeAll(Arrays.asList("", null));
  64.         }
  65.         catch (Exception e) {}
  66.         finally
  67.         {
  68.             if (readeFile!=null) {
  69.                 try
  70.                 {
  71.                     readeFile.close();
  72.                 }
  73.                 catch (IOException e) {}
  74.             }
  75.         }
  76.         return myNums;
  77.     }
  78.  
  79.  
  80.     public static void writeToFile (String outPut, ArrayList<String>nums) {
  81.         FileWriter fileWriter = null;
  82.         try
  83.         {
  84.             fileWriter = new FileWriter(outPut);
  85.             for (String s : nums) {
  86.                 fileWriter.write((Integer.parseInt(s)+" "));
  87.             }
  88.             fileWriter.close();
  89.         }
  90.         catch (Exception e) {}
  91.         finally
  92.         {
  93.             if (fileWriter!=null) {
  94.                 try
  95.                 {
  96.                     fileWriter.close();
  97.                 }
  98.                 catch (IOException e) {}
  99.             }
  100.         }
  101.     }
  102.  
  103.     public static boolean isNumeric(String str)
  104.     {
  105.         try
  106.         {
  107.             double d = Integer.parseInt(str);
  108.         }
  109.         catch(NumberFormatException nfe)
  110.         {
  111.             return false;
  112.         }
  113.         return true;
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement