/** java Show filename : shows the content of the file */ import java.io.*; public class Show { public static void main(String[] args) { String filename = null; FileInputStream in = null; if(args.length != 1) { System.out.println("Usage: java Show filename"); System.out.println("Shows the file"); return; // or System.exit(0); } else { filename = args[0]; try { in = new FileInputStream(filename); } catch (FileNotFoundException ex) { System.out.println(filename + " not found"); return; } int ch; try { while((ch = in.read()) != -1) { // process the char ch System.out.print((char)ch); } } catch (IOException ex) { // do nothing } try { in.close(); } catch (IOException ex) { // do nothing } } } } |
import java.io.*; public class FileOutTest { public static void main(String[] args) { char[] array = {'h', 'e', 'l', 'l', 'o'}; // hello String outFileName = "out.txt"; FileOutputStream out = null; try { out = new FileOutputStream(outFileName); } catch (FileNotFoundException ex) { return; // stop the program, probably should print out an error message } try { for(int i = 0; i < array.length; i++) { out.write(array[i]); } } catch (IOException ex) { // do nothing } try { out.close(); } catch (IOException ex) { // do nothing } } } |
import java.io.*; public class CopyFile { public static void main(String[] args) { String inFileName, outFileName; FileInputStream in = null; FileOutputStream out = null; if(args.length != 2) { System.out.println("Usage: java CopyFile inFile outFile"); System.out.println("copies inFile to outFile"); } else { inFileName = args[0]; outFileName = args[1]; try { in = new FileInputStream(inFileName); } catch (FileNotFoundException ex) { System.out.println(inFileName + " not found"); System.exit(0); } try { out = new FileOutputStream(outFileName); } catch (FileNotFoundException ex) { System.out.println("Can't open " + outFileName); System.exit(0); } try { int ch; while((ch = in.read()) != -1) { out.write(ch); } } catch (IOException ex) { System.out.println("IOException"); } // close all the stream try { in.close(); out.close(); } catch (IOException ex) { // do nothing } } } } |
import java.io.*; public class CopyFile2 { public static void main(String[] args) throws Exception { String inFileName, outFileName; FileInputStream in = null; FileOutputStream out = null; if(args.length != 2) { System.out.println("Usage: java CopyFile2 inFile outFile"); System.out.println("copies inFile to outFile"); } else { inFileName = args[0]; outFileName = args[1]; in = new FileInputStream(inFileName); out = new FileOutputStream(outFileName); int ch; while((ch = in.read()) != -1) { out.write(ch); } in.close(); out.close(); } } } |
import java.io.*; /** the program creates a log table */ public class LogTable { public static void main(String[] args) { PrintStream out = null; int max = 10; try { out = new PrintStream(new FileOutputStream("table")); } catch (FileNotFoundException ex) { System.exit(0); } // notice that no exception is thrown by print or println method for(int i = 1; i < max; i++) { out.println(i + "\t" + Math.log(i)); } out.close(); } } |
1 0.0 2 0.6931471805599453 3 1.0986122886681096 4 1.3862943611198906 5 1.6094379124341003 6 1.791759469228055 7 1.9459101490553132 8 2.0794415416798357 9 2.1972245773362196 |