Results 1 to 12 of 12
Thread: PrintWriter doesnt work :(
- 01-17-2010, 04:04 PM #1
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
PrintWriter doesnt work :(
Idk whats wrong, but it doesnt give an exception
nor does it, when i check the file later on, write anything to the file!
Idk why it doesnt work, heres my code:
It didnt work while using pw.flush and close so therefore they are in commented..Java Code:public static void saveMap(String mapName,Node rootNode){ PrintWriter pw; try { pw = new PrintWriter(new FileWriter(MainClass.dir+mapName)); List<Spatial> list = rootNode.getChildren(); for (int x = 0;x<list.size();x++){ Spatial obj = list.get(x); System.out.println(obj.getName()); if (!obj.getName().equals("skybox")){ float X = obj.getWorldTranslation().x; float Y = obj.getWorldTranslation().y; float Z = obj.getWorldTranslation().z; float sX = obj.getWorldScale().x; float sY = obj.getWorldScale().y; float sZ = obj.getWorldScale().z; String line = sX+","+sY+","+sZ+","+X+","+Y+","+Z+",1"; pw.println(line); System.out.println(line); } } //pw.flush(); //pw.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
- 01-17-2010, 04:09 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
Is there anything in your list? Check that. btw, you do have to close that PrintWriter, there is no need to explicitly flush it, close() will take care of that.
kind regards,
Jos
-
Please create and post a small compilable program that demonstrates your problem and that allows us to see it first hand, an SSCCE.
Also, out of respect to those here for which English is not a first language, and out of respect to us old fuddy-duddy's who bristle at it, please avoid using non-standard abbreviations such as "IDK". Thanks for your cooperation and much luck!
- 01-17-2010, 04:17 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
- 01-17-2010, 04:20 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
Okej, I tried to only use close, but it didnt work either..
So I made this small compilable class for you to test:
Hope this one works :)Java Code:public class test{ public static String dir = "C://Your file path"; public static void main(String args[]){ saveMap(); } public static void saveMap(){ PrintWriter pw; try { pw = new PrintWriter(new FileWriter(dir)); int[] list = {1,3,2,3}; for (int x = 0;x<list.length;x++){ String line = String.valueOf(list[x]); pw.println(line); System.out.println(line); } } //pw.flush(); pw.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
-
Did you test this class to see if it compiles before you ran it through your creative indentation utility and posted it here?
-
Corrected, this works fine for me:
Java Code:import java.io.FileWriter; import java.io.PrintWriter; public class test { // TODO: change this to appropriate directory!!! public static String dir = "C:/Users/Pete/Documents/Fubar/f1/foo001.txt"; public static void main(String args[]) { saveMap(); } public static void saveMap() { PrintWriter pw = null; try { pw = new PrintWriter(new FileWriter(dir)); int[] list = {1, 3, 2, 3}; for (int x = 0; x < list.length; x++) { String line = String.valueOf(list[x]); pw.println(line); System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } finally { if (pw != null) { //pw.flush(); pw.close(); } } } }Last edited by Fubarable; 01-17-2010 at 04:31 PM. Reason: NOBODY expects the Spanish Inquisition!
- 01-17-2010, 04:44 PM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
Oh sry about that, Thanks for correcting it :)
Really fast replys on this forum I must say!
Thanks so much for all help but I realised that the filepath was incorrect, and odd enough that didnt cause any exception -.-
But thanks alot for all support :D
-
welcome
No, it won't cause an exception -- you're not trying to read from a non-existing file but rather to create a non-existing file. So look at your hard drive and you may see a couple of new folders and a file that corresponds to the original file path.Thanks so much for all help but I realised that the filepath was incorrect, and odd enough that didnt cause any exception -.-
- 01-17-2010, 05:30 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
-
- 01-17-2010, 05:59 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
mysql connect button doesnt work quite right
By 711groove in forum New To JavaReplies: 0Last Post: 12-13-2009, 07:01 AM -
why doesnt my insertion sort method not work?
By Jeremy8 in forum New To JavaReplies: 7Last Post: 11-15-2009, 02:56 AM -
repaint class doesnt work anymore... dunno why..
By Addez in forum New To JavaReplies: 9Last Post: 11-07-2009, 09:10 PM -
Dll Call doesnt work
By INFACT in forum New To JavaReplies: 1Last Post: 10-04-2009, 09:31 PM -
java doesnt allow vista to work
By 10rosas in forum New To JavaReplies: 5Last Post: 12-22-2008, 04:23 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks