简体   繁体   English

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

[英]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 我有一个将Excel转换为CSV的程序,并且我使用了两种方法来做到这一点:一种使用CsvWriter写入,另一种使用BufferedWriter写入,但是我在这里遇到的问题是为了将Integer写入文件中需要将其转换为字符串

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 但是我需要一个纯Integer而不是一个字符串,因为当我尝试在Oracle数据库中上载此文件时,它会抛出

Ora-01722 invalid number exception ORA-01722无效的编号异常

I tried to create a CSV file from my Windows and Oracle works perfectly fine with that data. 我试图从Windows创建CSV文件,Oracle对该数据进行了完美的工作。

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. 使用DataOutputStream对象将原始int值写入文件。

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类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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