简体   繁体   中英

Is there a way to write integer to a file in 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();

一种选择是使用DataOutputStream(resp。DataInputStream)向文件中写入(或从文件中读取)任何原始Java类型。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM