I'm sure there is a better way of doing this conversion. Any suggestions?
package com.codegym.task.task18.task1820;
/*
Rounding numbers
*/
import java.io.*;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String file1 = scanner.nextLine();
String file2 = scanner.nextLine();
scanner.close();
//test files
// String file1 = "C:\\Users\\Andrew Cate\\CodeGym\\CodeGymTasks\\2.JavaCore\\src\\com\\codegym\\task\\task18\\task1820\\file1.txt";
// String file2 = "C:\\Users\\Andrew Cate\\CodeGym\\CodeGymTasks\\2.JavaCore\\src\\com\\codegym\\task\\task18\\task1820\\file2.txt";
FileInputStream is = null;
FileOutputStream os = null;
try {
//input and output streams
is = new FileInputStream(file1);
os = new FileOutputStream(file2);
//read from file1 and put into array
byte[] input = new byte[is.available()];
is.read(input);
//convert array to String
String inputString = "";
for (byte b : input) {
inputString += (char)b;
}
//convert String to array w/o spaces
String[] stringArray = inputString.split(" ");
for (String s : stringArray) {
//convert String to double
double d = Double.parseDouble(s);
//round double to nearest number
int i = (int)Math.round(d);
//writes negative numbers to file
if (i < 0) {
i = Math.abs(i);
os.write('-');
}
//writes positive numbers to file
os.write('0' + i);
os.write(' ');
}
} catch (FileNotFoundException e) {
System.out.println("File " + file1 + " not found.");
} catch (IOException e) {
e.printStackTrace();
} finally {
String closeMessage = "Could not close stream.";
try { if (is != null) is.close(); } catch (IOException e) {
System.out.println(closeMessage);
}
try { if (os != null) os.close(); } catch (IOException e) {
System.out.println(closeMessage);
}
}
}
}