Results 1 to 2 of 2
  1. #1
    jason27131 is offline Member
    Join Date
    Jul 2007
    Posts
    18
    Rep Power
    0

    Default help with file reader

    how do i actually run file reader?
    i know i have to create a new object, but what do i
    enter as the argument? This is the code.

    Code:
    import java.io.*;


    public class FileAccess {


    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();
    }
    }

    ty very much

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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);
        }
    }

Similar Threads

  1. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM
  2. Replies: 2
    Last Post: 04-07-2009, 06:25 AM
  3. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM
  4. A simple DOM reader
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-03-2008, 09:24 AM
  5. Converting text file(.txt) to JPG file(.jpg) in java
    By javadeveloper in forum Advanced Java
    Replies: 0
    Last Post: 11-09-2007, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •