如何将文本文件中的字符串分配给Java的整数数组?
如何将文本文件中的字符串分配给Java的整数数组? 我已将整数数组保存到文本文件中,但现在如何从文本文件中检索所有整数数组而不进行任何修改? 我希望检索到的整数数组与存储到文本文件之前相同。 以下是我的代码的一部分:(
BufferedWriter f1 = new BufferedWriter(new InputStreamReader(new FileInputStream("Input.txt")));
int a[ ] = yes(test, test.length); //go to yes method,return value to array
for(int i = 0; i < a.length; i++)
{
f1.write(a[i]);
}
f1.close( );
BufferedReader f2 = new BufferedReader(new InputStreamReader(new FileInputStream("Input.txt")));
String src;
while((src = f2.readLine( )) != null)
{
String[ ] s = src;
int a[ ] = Integer.parseInt(s);//same with before it saved
... ...
}
发现不兼容的类型)
从文本文件中保存和检索后如何保留整数数组(a[])的原创性? 谢谢!
How to assign a String from a text file into integer array for Java?
I had saved the integer array into a text file, but now how can I retrieve all the integer array from the text file without any modification? I want the integer array retrieved same as before it stored into the text file. Below is part of my code:
BufferedWriter f1 = new BufferedWriter(new InputStreamReader(new FileInputStream("Input.txt")));
int a[ ] = yes(test, test.length); //go to yes method,return value to array
for(int i = 0; i < a.length; i++)
{
f1.write(a[i]);
}
f1.close( );
BufferedReader f2 = new BufferedReader(new InputStreamReader(new FileInputStream("Input.txt")));
String src;
while((src = f2.readLine( )) != null)
{
String[ ] s = src;
int a[ ] = Integer.parseInt(s);//same with before it saved
... ...
}
(incompatible types found)
How to reserve the originality of the integer array(a[ ]) after saved and retrieved from the text file? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这看起来像是家庭作业。 无论如何,这是我的测试代码:
输出如下
This looks like homework. Anyway, here's my test code:
The output is as follows
您需要以某种方式分隔整数。 最简单的解决方案是将每个写在单独的行中。 或者使用逗号分隔列表,然后使用 String.split(",")
You need to somehow separate the integers. The simplest solution is to write each one in a separate line. Or use a comma separated list and then use String.split(",")
您编写字符的方式是完全错误的,您正在编写 int 的字符表示形式。 在内部,它被写成一个字符。 你需要像这样写你的整数。
这应该与您现在读取整数的方式兼容。
The way you have written the characters is totally wrong, you are writing the character representation of the int. Internally this is written as a char. You need to write your integers like so.
This should be compatible with how you are now reading your integers.