Results 1 to 12 of 12
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default 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:
    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();
    		}
    
    	}
    It didnt work while using pw.flush and close so therefore they are in commented..

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    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

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    old fuddy-duddy's
    idk what you mean by that; now confess, you rapscallion; what are they? Confess!

    kindest regards,

    Jos (aka The Spanish Inquisition ;-)

  5. #5
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Okej, I tried to only use close, but it didnt work either..

    So I made this small compilable class for you to test:
    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();
    		}
    
    	}
    }
    Hope this one works :)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Did you test this class to see if it compiles before you ran it through your creative indentation utility and posted it here?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  8. #8
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    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

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Addez View Post
    Oh sry about that, Thanks for correcting it :)
    Really fast replys on this forum I must say!
    welcome
    Thanks so much for all help but I realised that the filepath was incorrect, and odd enough that didnt cause any exception -.-
    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.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    A FileWriter doesn't create new folders if at least one of them doesn't exist in the given path.

    kind regards,

    Jos

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    A FileWriter doesn't create new folders if at least one of them doesn't exist in the given path.
    You are of course right and I'm wrong. Sorry about the misinformation.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    You are of course right and I'm wrong. Sorry about the misinformation.
    No problem; I'll tell the Spanish Inquisition about it so they can pay you a visit shortly ;-)

    kindest regards,

    Jos

Similar Threads

  1. mysql connect button doesnt work quite right
    By 711groove in forum New To Java
    Replies: 0
    Last Post: 12-13-2009, 07:01 AM
  2. why doesnt my insertion sort method not work?
    By Jeremy8 in forum New To Java
    Replies: 7
    Last Post: 11-15-2009, 02:56 AM
  3. repaint class doesnt work anymore... dunno why..
    By Addez in forum New To Java
    Replies: 9
    Last Post: 11-07-2009, 09:10 PM
  4. Dll Call doesnt work
    By INFACT in forum New To Java
    Replies: 1
    Last Post: 10-04-2009, 09:31 PM
  5. java doesnt allow vista to work
    By 10rosas in forum New To Java
    Replies: 5
    Last Post: 12-22-2008, 04:23 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
  •