如何将文本文件中的字符串分配给Java的整数数组?

发布于 2024-07-15 09:54:30 字数 741 浏览 2 评论 0原文

如何将文本文件中的字符串分配给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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆蝶 2024-07-22 09:54:30

这看起来像是家庭作业。 无论如何,这是我的测试代码:

// Write integers to a file, each on a different line
BufferedWriter f1 = new BufferedWriter( new FileWriter( new File( "Input.txt" ) ) );
int a[] = new int[]{ 17 , 42 , 37 };
for ( int i = 0 ; i < a.length ; i++ ) {
  f1.write( String.valueOf( a[ i ] ) + "\r\n" );
}
f1.close();

// Read integers from a file, assume each value on a different line
final BufferedReader bufferedReader = new BufferedReader( new FileReader( new File( "Input.txt" ) ) );
String line;
final List< Integer > values = new ArrayList< Integer >();
while ( ( line = bufferedReader.readLine() ) != null ) {
  values.add( Integer.valueOf( line ) );
}
bufferedReader.close();

// Convert List elements to array
final int[] valueArray = new int[ values.size() ];
int counter = 0;
for ( int value : values ) {
  valueArray[ counter ] = value;
  counter++;
}

// Print array values
for ( int value : valueArray ) {
  System.out.println( "value: |" + value + "|" );
}

输出如下

value: |17|
value: |42|
value: |37|

This looks like homework. Anyway, here's my test code:

// Write integers to a file, each on a different line
BufferedWriter f1 = new BufferedWriter( new FileWriter( new File( "Input.txt" ) ) );
int a[] = new int[]{ 17 , 42 , 37 };
for ( int i = 0 ; i < a.length ; i++ ) {
  f1.write( String.valueOf( a[ i ] ) + "\r\n" );
}
f1.close();

// Read integers from a file, assume each value on a different line
final BufferedReader bufferedReader = new BufferedReader( new FileReader( new File( "Input.txt" ) ) );
String line;
final List< Integer > values = new ArrayList< Integer >();
while ( ( line = bufferedReader.readLine() ) != null ) {
  values.add( Integer.valueOf( line ) );
}
bufferedReader.close();

// Convert List elements to array
final int[] valueArray = new int[ values.size() ];
int counter = 0;
for ( int value : values ) {
  valueArray[ counter ] = value;
  counter++;
}

// Print array values
for ( int value : valueArray ) {
  System.out.println( "value: |" + value + "|" );
}

The output is as follows

value: |17|
value: |42|
value: |37|
姐不稀罕 2024-07-22 09:54:30

您需要以某种方式分隔整数。 最简单的解决方案是将每个写在单独的行中。 或者使用逗号分隔列表,然后使用 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(",")

蓝天 2024-07-22 09:54:30

您编写字符的方式是完全错误的,您正在编写 int 的字符表示形式。 在内部,它被写成一个字符。 你需要像这样写你的整数。

for(int i = 0; i < a.length; i++)
{
   String value = String.valueOf(a[i]);
   f1.println(value);
}

这应该与您现在读取整数的方式兼容。

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.

for(int i = 0; i < a.length; i++)
{
   String value = String.valueOf(a[i]);
   f1.println(value);
}

This should be compatible with how you are now reading your integers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文