[英]How can I write to a file using BufferedWriter if the input type is double?
我正在尝试使用BufferedWriter
写入文件。 由于 write 方法采用int
参数,我将double
类型的输入转换为int
类型,但它没有被正确写入文件。
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();
}
我尝试将输入读取为int
file.nextInt()
但它也不起作用。
BufferedWriter
无法写入doubles
值,请参阅API 规范。 但是,您可以将double (file.nextDouble())
转换为String
,例如使用Double.toString((file.nextDouble()))
,然后使用write(String)
写入结果字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.