Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level19.lesson05.task03;
- /* Выделяем числа
- Считать с консоли 2 имени файла.
- Вывести во второй файл все числа, которые есть в первом файле.
- Числа выводить через пробел.
- Закрыть потоки. Не использовать try-with-resources
- Пример тела файла:
- 12 text var2 14 8v 1
- Результат:
- 12 14 1
- */
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Arrays;
- public class Solution {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String fileName = reader.readLine();
- String outFile = reader.readLine();
- writeToFile(outFile,getNums(fileName));
- reader.close();
- }
- public static ArrayList <String> getNums (String sourceFile){
- ArrayList<String>myNums = null;
- try
- {
- myNums = new ArrayList<String>();
- BufferedReader readeFile = new BufferedReader(new FileReader(sourceFile));
- String tmp="";
- while ((tmp=readeFile.readLine())!=null){
- String[]strings = tmp.split(" ");
- for (String s : strings){
- if (isNumeric(s)) {
- myNums.add(s);
- }
- }
- }
- readeFile.close();
- myNums.removeAll(Arrays.asList("", null));
- }
- catch (IOException e) {}
- return myNums;
- }
- public static void writeToFile (String outPut, ArrayList<String>nums) {
- FileWriter fileWriter = null;
- try
- {
- fileWriter = new FileWriter(outPut);
- int sizeOf = nums.size();
- for (int i = 0; i<sizeOf;i++) {
- fileWriter.write(nums.get(i)+" ");
- }
- fileWriter.close();
- }
- catch (IOException e) {}
- }
- public static boolean isNumeric(String str)
- {
- try
- {
- int d = Integer.parseInt(str);
- }
- catch(NumberFormatException nfe)
- {
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement