I am trying to write to a file using BufferedWriter. As the write method takes int argument, I am casting input of double type into int type but it is not being written properly into the file.
try{
Scanner file = new Scanner(new File("/home/amit/Desktop/number.csv"));
file.useDelimiter(", *");
BufferedWriter writer = new BufferedWriter(new FileWriter("/home/amit/Desktop/output_number.csv"));
while(file.hasNextDouble()){
writer.write((int)(file.nextDouble()));
}
writer.flush();
writer.close();
}catch(Exception x){
x.printStackTrace();
}
I tried reading the input as int that, file.nextInt() but it does not work either.
doubletointtruncates the decimal part.DataInputStreamfor reading).writer.write(file.next());would do. Besides that, you should use try-with-resources