public static void FileIO(String filename){ FileInputStream fis = null; try { fis = new FileInputStream(filename+".in"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.setIn(fis); PrintStream ps = null; try { ps = new PrintStream(new FileOutputStream(filename+".out")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.setOut(ps); }
其中try语句可以让eclipse自动补全。
配套赠送cin/cout版Java,令人怀念的C++。
import java.io.*; import java.util.*; import java.math.*; public class Main { public static void solve(Scanner cin,PrintStream cout){ while(cin.hasNext()){ int n=cin.nextInt(); cout.print(n); } } public static void main(String[] args) { //setFileIO("D://test"); Scanner cin=new Scanner(System.in); PrintStream cout=new PrintStream(System.out); solve(cin,cout); cin.close(); cout.close(); } public static void FileIO(String filename){ FileInputStream fis = null; try { fis = new FileInputStream(filename+".in"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.setIn(fis); PrintStream ps = null; try { ps = new PrintStream(new FileOutputStream(filename+".out")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.setOut(ps); } }