筱海 发表于 2022-5-6 19:48:02

从文件1.txt读取的第三个和第五个整数,求和 (0<和<10000) 输出显示。

从文件1.txt读取的第三个和第五个整数,求和 (0<和<10000) 输出显示。

Twilight6 发表于 2022-5-6 21:55:15


数据是用空格隔开的么?参考代码:

import java.io.FileInputStream;
import java.io.IOException;

public class TestFileWrite {

    public static void main(String[] args) {
      FileInputStream fis = null;
      try {
            fis = new FileInputStream("1.txt");

            int len;
            byte[] data = new byte;
            StringBuilder temp = new StringBuilder();
            while ((len = fis.read(data)) != -1){
                temp.append(new String(data, 0, len));
            }
            String text = new String(temp);
            String[] result = text.split(" ");
            System.out.println(Integer.parseInt(result) + Integer.parseInt(result));
            
      } catch(Exception e) {
            e.printStackTrace();
      } finally {
            if (fis != null){
                try {
                  fis.close();
                } catch (IOException e) {
                  e.printStackTrace();
                }
            }
      }
    }
}
页: [1]
查看完整版本: 从文件1.txt读取的第三个和第五个整数,求和 (0<和<10000) 输出显示。