Results 1 to 4 of 4
Thread: Import java.io.* problem
- 07-28-2009, 09:16 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 5
- Rep Power
- 0
[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();
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.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'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.
- 07-28-2009, 09:45 PM #2
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.
- 07-28-2009, 10:36 PM #3
Member
- Join Date
- Jul 2009
- Posts
- 5
- Rep Power
- 0
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.
- 07-28-2009, 10:45 PM #4
Member
- Join Date
- Jul 2009
- Posts
- 35
- Rep Power
- 0
Similar Threads
-
error with import java.io.*
By osval in forum New To JavaReplies: 5Last Post: 05-13-2010, 05:03 PM -
[SOLVED] import netscape.javascript.* problem
By shwein in forum New To JavaReplies: 2Last Post: 10-30-2008, 06:43 PM -
import java.util ????
By kris09 in forum New To JavaReplies: 3Last Post: 08-11-2008, 12:39 AM -
Simple newbie problem: import 3rd party jar
By jodyflorian in forum EclipseReplies: 5Last Post: 05-30-2008, 04:02 AM -
how to import jface in java swt
By rajaletchumy in forum New To JavaReplies: 1Last Post: 08-08-2007, 12:31 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks