View Single Post
  #2 (permalink)  
Old 08-01-2007, 04:03 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
Code:
import java.io.*; public class FileAccessRx { public static void displayFile(String f) throws IOException { FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); String line = br.readLine(); while (line != null) { System.out.println(line); line = br.readLine(); } br.close(); fr.close(); } public static void writeToFile(String outfile, String s) throws IOException { FileOutputStream fs = new FileOutputStream(outfile); PrintStream p = new PrintStream(fs); p.println(s); p.close(); fs.close(); } public static void main(String[] args) throws IOException { // Your methods have the static modifier so you can // access/call them like this: FileAccessRx.displayFile("FileAccessRx.java"); FileAccessRx.writeToFile("staticAccess.txt", "Hello World"); // Or if you like you can use a reference to the class // to access/call them. FileAccessRx fileAccess = new FileAccessRx(); String s = "Calling a static method using an instance variable."; fileAccess.writeToFile("instanceAccess.txt", s); } }
Reply With Quote