|
发表于 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[1024];
- 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[2]) + Integer.parseInt(result[4]));
-
- } catch(Exception e) {
- e.printStackTrace();
- } finally {
- if (fis != null){
- try {
- fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
复制代码 |
|