OJ中java,C++输入输出重定向的方法

Java中的重定向比C/C++ (freopen)麻烦一些


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        // 输入重定向
        BufferedInputStream in = new BufferedInputStream(new FileInputStream("std.in"));
        System.setIn(in);
        Scanner stdin = new Scanner(System.in);
        int a = stdin.nextInt();
        int b = stdin.nextInt();
        // 输出重定向
        PrintStream out = new PrintStream(new FileOutputStream("estdout.pc2"));
        System.setOut(out);
        System.out.print(a + b);
        out.close(); // 关闭重定向
    }
}



下面是C/C++的重定向

#ifndef ONLINE_JUDGE
	freopen("S3.in", "r", stdin);
	freopen("S3.out", "w", stdout); 
#endif





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值