Cách đọc file input và write file input - Java

Tạo file input.txt. Sau khi chạy xong code nó sẽ tự động sinh ra file output

5

1 2

2 3

3 4

4 5

5 6

Và dưới đây là code:

  1. import java.io.FileWriter;
  2. import java.io.File;
  3. import java.util.Scanner;
  4. import java.math.BigInteger;
  5. public class nhap{
  6. public static void main(String[] args){
  7. try {
  8. /* Input*/
  9. File myObj = new File("input.txt");
  10. Scanner myReader = new Scanner(myObj);
  11. Integer testcase = myReader.nextInt();
  12. /*Output */
  13. FileWriter myobj_out = new FileWriter("output1.txt");
  14. // Integer ans = 5;
  15. // Integer ans1 = 6;
  16. // myobj_out.write(ans.toString());
  17. // myobj_out.write('\n');
  18. // myobj_out.write(ans1.toString());
  19. /*Solve */
  20. for (Integer i = 0; i < testcase; i++) {
  21. // String data = myReader.nextLine();
  22. BigInteger a = myReader.nextBigInteger();
  23. BigInteger b = myReader.nextBigInteger();
  24. BigInteger c = a.add(b);
  25. System.out.println(c);
  26. myobj_out.write(c.toString());
  27. myobj_out.write('\n');
  28. }
  29. myobj_out.close();
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }

Nhận xét

Bài đăng phổ biến từ blog này

Sinh Test trong Python va code AC

Cách tính a*b mod m , a^b mod m , a^(-1) mod m với m không phải là số nguyên tố (inversion có nghĩa khi (a,m)=1)

Python - Liệt kê tất các các chu trình trong đồ thị