Trở về đầu

foto1 foto2 foto3 foto4 foto5
Giảng viên
Nguyễn Tô Sơn - Thủ khoa Đại học Sư phạm Hà Nội
ĐT: 091.333.2869

HỌC TIN CÙNG THỦ KHOA

Thành công không phải đích đến, mà là cả một hành trình

Tìm kiếm

Đề bài: Nhập xuất file trong Java.

Mời các bạn download tại: Click here

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;


public class FILE {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException{
		// TODO Auto-generated method stub

		FileReader InputStream = null;
		FileWriter OutputStream = null;

		int c;

		try {
			InputStream = new FileReader("fileINP.txt");
			OutputStream = new FileWriter("fileOUT.txt");
			Scanner scan = new Scanner(InputStream);
			PrintWriter output = new PrintWriter(OutputStream, true);

			c = scan.nextInt();
			output.print(" "+c);
			System.out.println(c);

			c = scan.nextInt();
			output.print(" "+c);
			System.out.println(c);

			c = scan.nextInt();
			output.print(" "+c);
			System.out.println(c);

			c = scan.nextInt();
			output.print(" "+c);
			System.out.println(c);


		}
		catch (Exception e){
			System.out.println("Co loi nhap xuat file: " + e.toString());

		}
		finally {
			if (InputStream != null)
			{
				InputStream.close();
				System.out.println("Doc thanh cong!");
			}
			if (OutputStream != null)
			{
				OutputStream.close();
				System.out.println("Ghi thanh cong!");
			}
		}
	}
}

Giảng viên Nguyễn Tô Sơn, Thủ khoa Đại học Sư phạm Hà Nội. Điện thoại: 091.333.2869