Results 1 to 3 of 3
  1. #1
    edi.gotieb is offline Member
    Join Date
    Apr 2010
    Posts
    12
    Rep Power
    0

    Exclamation please help! little IO problem!

    im trying to write a program that uses objects (iO) to do io functions with specific files. so i creat a class (iO): in the constructor i receive only the file
    directory- the object then creates some public streams and has a bunch of methods that can read or write to the file. i also have a method that closes all the streams. for some weird reason that i fail to understand when i call on my readline method from my main class through an object of type iO that i instantiated the method deletes all thats in the specified folder and then returns null- instead of the line that was in the file.
    my iO class looks something like this:

    import java.io.*;
    public class iO {
    public BufferedReader bfr;
    public BufferedWriter bfw;
    public FileReader fr;
    public FileWriter fw;
    iO(String a) throws Exception {
    fr= new FileReader(a);
    bfr= new BufferedReader(fr);
    fw= new FileWriter(a);
    bfw= new BufferedWriter(fw);
    }
    public String readDBL() {
    try {
    String str;
    str=bfr.readLine();
    return str;
    }
    catch (Exception e) {
    e.printStackTrace();
    return null;
    } }

    public void writeDBL(String a) {
    try {
    for (int i=0; i<a.length(); i++) {
    bfw.write(a.charAt(i));
    }
    bfw.newLine(); }
    catch (Exception ex) {
    ex.printStackTrace();
    } }
    public void clostStreams() throws IOException {
    bfr.close();
    bfw.close();
    fr.close();
    fw.close();
    }}
    The main class looks like this:
    import java.io.*;
    public class main {


    public static void main(String[] args) {
    try {
    iO userReader=new iO("C:\\Users\\Edi's Mean Machine\\Documents"+ "\\123.txt");
    System.out.println(userReader.readDBL());
    userReader.clostStreams();
    }

    catch (Exception e) {
    e.printStackTrace();
    } } }


    Can some one Please explain thoroughly why this doesn't work?
    thanks
    Edi

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Please use CODE tags and format your code properly so that we can understand it.
    Java Code:
    import java.io.*;
    
    public class iO {
    Start your class names with an upper case letter. This one should be IO, but generally letters after the first will be lower case unless they start a new word -- like TrayTable or Sofa.
    Java Code:
        public BufferedReader bfr;
        public BufferedWriter bfw;
        public FileReader fr;
        public FileWriter fw;
    Get in the habit of making your instance variables private unless you have a good reason to make them public. You want code outside your class to use your published API -- the public methods you declare -- and not to go mucking around with your instance variables. Also, you don't need to persist fr and fw, as they are only used in instantiating bfr and bfw.
    Java Code:
        iO(String a) throws Exception {
            fr = new FileReader(a);
            bfr = new BufferedReader(fr);
            fw = new FileWriter(a);
            bfw = new BufferedWriter(fw);
        }
    You're opening the same file for reading and writing at the same time? This is what is causing your main symptom. You are overwriting the file by opening it for writing. Also, give your parameter a better name -- "a" doesn't mean anything, but fileName does. And you have forgotten your access modifier. You probably your constructor to be public, but without a modifier it is "package-protected".
    Java Code:
        public String readDBL() {
            try {
                String str;
                str = bfr.readLine();
                return str;
            }
            catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
        public void writeDBL(String a) {
            try {
                for (int i = 0; i < a.length(); i++) {
                    bfw.write(a.charAt(i));
    Why are we writing one character at a time? You may have confused yourself by reading the BufferedWriter API, but it inherits a write(String) method from Writer.
    Java Code:
                }
                bfw.newLine();
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
        public void clostStreams() throws IOException {
    Don't you want to call this method closeStreams()?
    Java Code:
            bfr.close();
            bfw.close();
            fr.close();
            fw.close();
        }
    }
    All in all, I think you'll find that you didn't gain much with this class (but it's certainly a good exercise to play with the java.io libraries). In the end, you'd end up having to do pretty much the same things with your iO class that you now have to do with the java.io libraries, so you may as well use them directly.
    Java Code:
    import java.io.*;
    
    public class main {
    Bad name -- call this class IOTester or something like that. Or since this looks like test code, you can put it right in your IO class.
    Java Code:
        public static void main(String[] args) {
            try {
                iO userReader = new iO("C:\\Users\\Edi's Mean Machine\\Documents"+ "\\123.txt");
                System.out.println(userReader.readDBL());
                userReader.clostStreams(); 
            }
            catch (Exception e) {
                e.printStackTrace();
            }
         }
    }
    Good luck!

    -Gary-

  3. #3
    edi.gotieb is offline Member
    Join Date
    Apr 2010
    Posts
    12
    Rep Power
    0

    Smile thank you very much!

    thanks,
    ive been searching for a solution for a very long time!
    ill take into account your comments :)


    thanks again

    Edi

Tags for this Thread

Posting Permissions

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