Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level18.lesson10.home05;
- /* Округление чисел
- Считать с консоли 2 имени файла
- Первый файл содержит вещественные(дробные) числа, разделенные пробелом. Например, 3.1415
- Округлить числа до целых и записать через пробел во второй файл
- Закрыть потоки. Не использовать try-with-resources
- Принцип округления:
- 3.49 - 3
- 3.50 - 4
- 3.51 - 4
- -3.49 - -3
- -3.50 - -3
- -3.51 - -4
- */
- import java.io.*;
- import java.util.ArrayList;
- import java.util.List;
- public class Solution {
- public static void main(String[] args) throws IOException{
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String fiename1 = reader.readLine();
- String fiename2 = reader.readLine();
- reader.close();
- fileToList(fiename1);
- byte[]someBytes = listToString(fiename1).getBytes();
- FileOutputStream fileOutputStream = new FileOutputStream(fiename2);
- fileOutputStream.write(someBytes);
- fileOutputStream.close();
- }
- public static int roundThis (double num) {
- return (int)Math.round(num);
- }
- /*-----------------------------------------------------------*/
- public static List<Integer> fileToList (String fileName) throws IOException{
- List<Integer> listWithInts = new ArrayList<Integer>();
- BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
- String sCurrentLine;
- while ((sCurrentLine=bufferedReader.readLine()) != null) {
- String [] strArg = sCurrentLine.split(" ");
- for (String str : strArg) {
- listWithInts.add(roundThis(Double.parseDouble(str)));
- }
- }
- bufferedReader.close();
- return listWithInts;
- }
- /*--------------------------------------------------------------*/
- public static String listToString (String someFileName) throws IOException{
- String arrayString = "";
- int listSize = fileToList(someFileName).size();
- for (int i = 0; i < fileToList(someFileName).size(); i++) {
- if(i < listSize-1) {
- arrayString += fileToList(someFileName).get(i);
- arrayString+=" ";}
- else
- arrayString += fileToList(someFileName).get(i);
- }
- return arrayString;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement