Results 1 to 4 of 4
  1. #1
    DD70 is offline Member
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Red face [SOLVED] Import java.io.* problem

    Why doesn't "import java.io.*;" import FileReader and FileWriter? It seems to do so for other classes.

    Unless I explicitly import FileWriter (ie: with "import java.io.FileWriter;"), the code below gives the following compiler errors:
    ImportTest.java:8: cannot find symbol
    symbol : constructor FileWriter(java.io.File)
    location: class FileWriter
    FileWriter fw = new FileWriter(f);

    ImportTest.java:9: cannot find symbol
    symbol : method close()
    location: class FileWriter
    fw.close();

    Java Code:
    import java.io.*;
    
    class ImportTest
    {
            public static void main(String[] args) throws IOException
            {
                    File f = new File("Test.txt");
                    FileWriter fw = new FileWriter(f);
                    fw.close();
            }
    }
    I could use java.io.File, java.io.FileWriter and java.io.IOException but I'm puzzled as to why the import doesn't appear to be working as I expect it to.

    I'm working through examples in a book and the code above is a stripped-down version of the example in the book which gives the same errors.

    What have I missed?
    Last edited by DD70; 07-28-2009 at 10:27 PM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,342
    Rep Power
    13

    Default

    Works for me, not sure what the deal is.

    However...

    You should import your classes explicitly. Importing * (everything) increases compile time a great deal. A good IDE (NetBeans, Eclipse) will do this for you automatically.

  3. #3
    DD70 is offline Member
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by quad64bit View Post
    Works for me, not sure what the deal is.

    However...

    You should import your classes explicitly. Importing * (everything) increases compile time a great deal. A good IDE (NetBeans, Eclipse) will do this for you automatically.
    Thanks for your help. I'm glad it works on your system, I was beginning to doubt what I thought I knew.

    It turns out I had a file "FileWriter.java" in the same location as my example code. :o Compiling with -verbose showed an import from "./FileWriter.java" rather than the same location as the other classes.

    Can't think how I managed to create a class with the same name as an existing one but it's useful to know that the compiler chooses that over the built-in classes.

    I'll have a look at the IDE's you mentioned, I'm still on text editors.

  4. #4
    adz
    adz is offline Member
    Join Date
    Jul 2009
    Posts
    35
    Rep Power
    0

    Default

    that's why you should use packages :)

    Well, one reason at least

Similar Threads

  1. error with import java.io.*
    By osval in forum New To Java
    Replies: 5
    Last Post: 05-13-2010, 05:03 PM
  2. [SOLVED] import netscape.javascript.* problem
    By shwein in forum New To Java
    Replies: 2
    Last Post: 10-30-2008, 06:43 PM
  3. import java.util ????
    By kris09 in forum New To Java
    Replies: 3
    Last Post: 08-11-2008, 12:39 AM
  4. Simple newbie problem: import 3rd party jar
    By jodyflorian in forum Eclipse
    Replies: 5
    Last Post: 05-30-2008, 04:02 AM
  5. how to import jface in java swt
    By rajaletchumy in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:31 AM

Posting Permissions

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