5-5

来自「java入门的几个代码,很实用,初学者不可错过」· 代码 · 共 35 行

TXT
35
字号
import java.io.*;
class sum{
public static void main(String args[])   //方法main()没有抛出异常
{
DataInputStream fis=null;
try             //try体可能有抛出的异常或程序错   
{
fis=new DataInputStream(new FileInputStream("myfile.txt"));//建立读入"myfile.txt"文件的输
//入流
String stra=fis.readln();  //读入一个字符串,.readln()抛出异常
int a=Integer.parseInt(stra);//把该字符串转化为整数
String strb=fis.readln();//读入一个字符串,.readln()抛出异常
int b =Integer.parseInt(strb); //把该字符串转化为整数
int c=a+b;
System.out.println(c.toString());
//输出c
fis.close();
}
catch(FileNotFoundException e)   //
{
System.out.println(e.toString());
}
catch(IOException e)
{
System.out.println(e.toString());
fis.close();
}
catch(NumberFormatException e)
{
System.out.println(e.toString());
fis.close();
}
}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?