1 2 Previous Next 20 Replies Latest reply on Feb 28, 2007 8:11 AM by masijade

    To save the output in a specified directory.

    807606
      Hi,

      I am running a java application (on Eclipse) and i am saving this output file usin "file dialog" methods. Howeve all the files are being saved in my workspace which i am using for Eclipse.. Even if i change the directory it saves in the workspace (of eclipse) itself...

      please someone help me out...
      Thanks in advance
        • 1. Re: To save the output in a specified directory.
          DrClap
          Most likely you are specifying a relative path for the file when you save it. Or even no path at all, which results in saving in the current working directory. Specify the full path.

          That's just a guess based on the code you posted (none).
          • 2. Re: To save the output in a specified directory.
            807606
            Even if i change the
            directory it saves in the workspace (of eclipse)
            itself...
            Instead of delving into changing path on eclipse properties, you can save the file in a dir of your choice by providing an absoulte path. e.g.
            File file = new File("d:\MyDir\temp.txt")
            . This will save file in the dir specified irrespective of the directory where you choose to execute the program.
            Else it will save in a dir relative to the present working directory of the project in Eclipse.
            • 3. Re: To save the output in a specified directory.
              807606
              I am sorry to ask again but in my case i have not defined any path...Its getting saved in Eclipse workspace automatically..

              Here is the code i am using..

                        try {
                             FileDialog fd = new FileDialog(f, "Save as PNG", FileDialog.SAVE);
                             fd.setFile("canvas.png");
                             fd.setVisible(true);
                             String name = fd.getFile();
                             Rectangle r = canvas.getBounds();
                             Image image = canvas.createImage(r.width, r.height);
                             Graphics g = image.getGraphics();
                             canvas.paint(g);
                             ImageIO.write((RenderedImage) image, "png", new File(name));
                        } catch (IOException ioe) {
                             ioe.printStackTrace();
                        }
              • 4. Re: To save the output in a specified directory.
                807606
                I am sorry to ask again but in my case i have not
                defined any path...Its getting saved in Eclipse
                workspace automatically..
                I guess you are executing a Java project using Eclipse IDE, which, by default creates file in the project workspace. If you copy the .class file of the program to another dir (say: d:\newDir) and issue java <mainClassFileName>, then you will find that the new file gets created under d:\newDir (and NOT under Eclipse workspace!) . As simple as that.

                Where exactly do you want your file to be placed ?
                • 5. Re: To save the output in a specified directory.
                  masijade
                  I am sorry to ask again but in my case i have not
                  defined any path...Its getting saved in Eclipse
                  workspace automatically..
                  I guess you are executing a Java project using
                  Eclipse IDE, which, by default creates file in the
                  project workspace. If you copy the .class file of the
                  program to another dir (say: d:\newDir) and issue
                  java <mainClassFileName>, then you will find that the
                  new file gets created under d:\newDir (and NOT under
                  Eclipse workspace!) . As simple as that.

                  Where exactly do you want your file to be placed ?
                  I'm sorry, but this is bad advice. You don't place your application binaries depending on where you want to save output. And it wouldn't necessarily help, anyway. What matters is his current working directory when he executes java, not where the class/jarfile is located. And this only matters when using a relative path or no path at all.

                  @OP: Your solution, as has been posted twice, is to use a full path. You can, with JFileChooser (if that is what you are using) specify this path as the default location to open in. I don't remember the method name, read the API doc for JFileChooser and you will find it. But as mentioned, it is the fact that you are not using a path. Use a full path and your problem goes away (or set the default in JFileChooser).

                  Edit:

                  Okay looked closer again and I see you are using FileDialog. But that also has setDirectoy method. Call that method using the full path of where you want to save files and this will solve your problem.
                  • 6. Re: To save the output in a specified directory.
                    807606
                    Again sorry to disturb you.. I resolved the problem with the help extended to me .. But i got another problem to resolve.... Is there any way by which instead of popping up the dialog window and pressing on button save , the file gets automatically saved. If so please tell me...

                    Thanks
                    piyush
                    • 7. Re: To save the output in a specified directory.
                      masijade
                      Of course there is. Simply create a filename string yourself and call the write method using that, rather than getting a filename string from the FileDialog.
                      • 8. Re: To save the output in a specified directory.
                        807606
                        Again sorry to disturb you.. I resolved the problem with the help extended to me .. But i got another problem to resolve.... Is there any way by which instead of popping up the dialog window and pressing on button save , the file gets automatically saved. If so please tell me...

                        Thanks
                        piyush
                        • 9. Re: To save the output in a specified directory.
                          807606
                          I am sorrry.. I didnt get you... Could you please explain little bit more...

                          Thanks
                          Piyush
                          • 10. Re: To save the output in a specified directory.
                            masijade
                            You are writing the file using this line
                            ImageIO.write((RenderedImage) image, "png", new File(name));
                            No file exists until this point, with "name" being a simple String containing the name of the file (preferably with the full path). Currently you are getting this string by using a FileDialog, but you could just simply do this yourself, by throwing away all the code that references the FileDialog and simply doing this:
                            String name = "D:/Bogus Path/Bogus.png";
                            ImageIO.write((RenderedImage) image, "png", new File(name));
                            • 11. Re: To save the output in a specified directory.
                              807606
                              I think i have disturbed you again.. Whatever suggestion u gave is very much applicabla..but what i want to have that without popping up the window (i.e without user action) how i will be able to save the file.

                              In other words i dont want any user initiated action clicking yes /no or save/cancel. I want file to be saved automatically.. However whatever the suggestion u gave is returning "Null Pointer Exception.."

                              so what sud i do..This is the complete code i have written...

                              import java.awt.*;
                              import java.awt.event.*;
                              import java.awt.image.*;
                              import java.io.*;
                              import javax.imageio.*;
                              import net.sourceforge.barbecue.linear.codabar.CodabarBarcode;
                              public class SaveImage_Barbecue {
                                   private static final long serialVersionUID = 1L;

                                   BufferedImage img;

                                   public SaveImage_Barbecue() {
                                        final Frame f = new Frame();
                                        final Canvas canvas = new Canvas() {
                                             public void paint(Graphics g) {
                                                  //This command is used to load a scaled image of img
                                                  //g.drawImage(img.getScaledInstance(450,600,0), 0, 0, null);
                                                  g.create(0, 0, 450, 600);
                                                  g.clearRect(0, 0, 450, 600);
                                                  g.setColor(Color.WHITE);
                                                  g.fillRect(0, 0, 450, 600);
                                                  g.setColor(Color.BLACK);

                                                  g.drawString("BANANA REPUBLIC", 10, 15);
                                                  g.drawString("5920 BANANA REPUBLIC BLVD", 10, 30);
                                                  g.drawString("GROVEPORT, OH - 43195-0003", 10, 45);
                                                  g.drawString("800-427-7895", 10, 60);

                                                  g.drawString("SHIP DATE: 11DEC2006", 250, 15);
                                                  g.drawString("ACTUAL WGT: 3.00 LBS", 250, 30);
                                                  g.drawString("DIMMED 0 x 0 x 0", 250, 45);

                                                  g.drawString("TO", 10, 150);
                                                  g.drawString("614-409-7153", 310, 165);
                                                  g.drawString("SATURDAY OVERNIGHT", 30, 180);
                                                  g.drawString("5150 HIGHGLADE DR", 30, 195);
                                                  g.drawString("HILLIARD, OH - 43195", 30, 210);

                                                  g.drawString("Delivery Address Barcode", 30, 280);

                                                  g.drawString("System# 663514 29OCT2004", 10, 370);
                                                  g.drawString("TRK# 6548 8206 9744 Form 0201", 10, 385);
                                                  g.drawString("Deliver By", 310, 370);
                                                  g.drawString("30Oct04", 310, 385);
                                             }
                                        };
                                        canvas.setBackground(Color.white);
                                        f.setSize(450, 600);
                                        f.setLayout(new BorderLayout());
                                        f.add(canvas, BorderLayout.CENTER);
                                        try {
                                                                      FileDialog fd = new FileDialog(f, "Save as PNG", FileDialog.SAVE);
                                             fd.setFile("canvas.png");
                                             fd.setVisible(true);
                                             
                                             Rectangle r = canvas.getBounds();
                                             Image image = canvas.createImage(r.width, r.height);
                                             Graphics g = image.getGraphics();
                                             canvas.paint(g);
                                             ImageIO.write((RenderedImage) image, "png", new File(name));
                                        } catch (IOException ioe) {
                                             ioe.printStackTrace();
                                        }
                                        f.addWindowListener(new WindowAdapter() {
                                             public void windowClosing(WindowEvent e) {
                                                  System.exit(0);
                                             }
                                        });
                                        f.setVisible(true);
                                   }

                                   public static void main(String[] args) {
                                        new SaveImage_Barbecue();
                                   }
                              }
                              • 12. Re: To save the output in a specified directory.
                                masijade
                                You should have removed this:
                                FileDialog fd = new FileDialog(f, "Save as PNG", FileDialog.SAVE);
                                fd.setFile("canvas.png");
                                fd.setVisible(true);
                                String name = fd.getFile();
                                but you only removed this:
                                String name = fd.getFile();
                                and you forgot to add this:
                                String name = "D:/Bogus Path/Bogus.png";
                                And, obviously, you need to replace the "D:/Bogus Path/Bogus.png" with the real path and file name you wish to use.

                                Message was edited by:
                                masijade.
                                • 13. Re: To save the output in a specified directory.
                                  807606
                                  Yes sir.. I am using exactly that code which u asked me to do.....That was the old code i was using.....

                                  Here is the code i am using. But it gives null pointer exception

                                            try {
                                            String name = "C:/Piyush/canvas.png";
                                            Rectangle r = canvas.getBounds();
                                            Image image = canvas.createImage(r.width, r.height);
                                            Graphics g = image.getGraphics();
                                            canvas.paint(g);
                                            ImageIO.write((RenderedImage) image, "png", new File(name));
                                  • 14. Re: To save the output in a specified directory.
                                    masijade
                                    Where? On what line?
                                    1 2 Previous Next