try { System.setOut(new PrintStream(new FileOutputStream("output.txt"))); System.out.println("Hello, world!"); } catch (FileNotFoundException e) { e.printStackTrace(); }
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { String input = br.readLine(); System.out.println("You entered: " + input); } catch (IOException e) { e.printStackTrace(); }
try { FileInputStream in = new FileInputStream("input.txt"); FileOutputStream out = new FileOutputStream("output.txt"); byte[] buffer = new byte[1024]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); }These examples demonstrate the use of java.io System in Java. The package library is java.lang.