java 写入文件整数,有没有办法在Java中将整数写入文件

I have a program that converts Excel to CSV and I used 2 ways to do this: one of them uses CsvWriter write and the other program uses BufferedWriter write but the problem that I have encountered here is in order to write an Integer to a file you need to convert it to string with

String.valueOf(myInt);

but I need a pure Integer not a string because when I am trying to upload this file in Oracle database it throws me

Ora-01722 invalid number exception

I tried to create a CSV file from my Windows and Oracle works perfectly fine with that data.

So my question is there a way to write an Integer not a String to a file? Any help?

解决方案

Use DataOutputStream Object to write primitive int value to a file.

Example:

//create FileOutputStream object

FileOutputStream fos = new FileOutputStream(strFilePath);

/*

* To create DataOutputStream object from FileOutputStream use,

* DataOutputStream(OutputStream os) constructor.

*/

DataOutputStream dos = new DataOutputStream(fos);

int i = 100;

/*

* To write an int value to a file, use

* void writeInt(int i) method of Java DataOutputStream class.

*

* This method writes specified int to output stream as 4 bytes value.

*/

dos.writeInt(i);

/*

* To close DataOutputStream use,

* void close() method.

*

*/

dos.close();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值