13 Replies Latest reply: Jan 22, 2008 2:50 AM by 807603 RSS

    My file is not saving

    807603
      Hello everyone

      I used
      String realPath =config.getServletContext().getRealPath("/");

      to get the real path of my application.

      I got the string as: /home/cascanicacom/public_html/


      Now I need to create a file "write.txt" and save it under the /home/cascanicacom/public_html/ on my web server


      Now please tell me where should I make the changes in the
      File file = new File("write.txt"); so as to create it in the server.

      I even tried:
      File file = new File(realPath +"write.txt");

      And when I checked in the server theres no file there.
      Please!
      Please advice me.
        • 1. Re: My file is not saving
          807603
          who said that File file = new File(realPath +"write.txt"); this line only creates new file
          public OutputStream out=null;
          public PrintStream p = null;
               File logFile = null;
                    try {           
                         logFile = new File("FilePath");
                         if ( !logFile.exists() ) {
                              out = new FileOutputStream(logFile);
                              p   = new PrintStream(out);
                              
                         } else {
                              out = new FileOutputStream(logFile,false);
                              p   = new PrintStream(out);
                         }
                    } catch( Exception e) {
                         e.printStaceTrace();
                    }
          u can use this !! this should work
          • 2. Re: My file is not saving
            807603
            Thanks Amit,

            Let me test it.
            I'll respond soon.
            • 3. Re: My file is not saving
              807603
              Sorry Amit,

              That did not help either.
              Its the same thing (unable to create the file in the web server).

              But thanks for the help.
              Thank you.
              • 4. Re: My file is not saving
                807603
                Only invoking new File() indeed doesn't create the physical file. You have to open the stream on the file first.

                Anyway, didn't you see any useful exceptions or traces after doing so? Are you looking in the right file location? Do you have the sufficient rights to write and view files? Do you understand how UNIX file systems work?
                • 5. Re: My file is not saving
                  807603
                  BalusC wrote:
                  Only invoking new File() indeed doesn't create the physical file. You have to open the stream on the file first.
                  Thank you Sir for the reply,
                  How can I open the file if the file doesn't exist on the web server. My job is to create the file through codding. Even if the file exist in the web server it will overwrite it completely.
                  >
                  Anyway, didn't you see any useful exceptions or traces after doing so?
                  Till now, there's no exception.
                  I tried using the
                  File file = new File(rootPath+"write.txt") //rootPath= getRealPath("/")
                  Are you looking in the right file location?
                  No, I am just trying to create a file in the web server under the /home/cascanicacom/public_html/
                  Do you have the sufficient rights to write and view files?
                  As its going to overwrite the file after every execution, so, once I create a file manually on the server and checked all the attributes of the file like read, write, etc. That was just for checking whether the reason was for the permission or not. But still the file didn't overwrite it. So that means the file just couldn't find the server path.
                  Do you understand how UNIX file systems work?
                  No, I don't have much idea at all.


                  Please help me out BalusC
                  • 6. Re: My file is not saving
                    807603
                    u r path must be wrong ,
                    File file = new File(rootPath+"/"+"URFileName.txt");
                    try this may be this will help u
                    • 7. Re: My file is not saving
                      807603
                      AmitChalwade123456 wrote:
                      u r path must be wrong ,
                      File file = new File(rootPath+"/"+"URFileName.txt");
                      try this may be this will help u
                      Sorry Amit, that didn't solve my problem.
                      Its something like this:

                      File file = new File("/home/cascanicacom/public_html//write.txt");

                      Is there any other way friend?
                      • 8. Re: My file is not saving
                        807603
                        Zoram wrote:
                        How can I open the file if the file doesn't exist on the web server.
                        Call an OutputStream or Writer on it.

                        Only
                        void doSomething() {
                            new File("c:/foo.txt");
                        }
                        won't create the actual file. It just creates a file location hook.
                        • 9. Re: My file is not saving
                          807603
                          File file = new File("/home/cascanicacom/public_html//write.txt");

                          Is there any other way friend?
                          see reply first !!! it should work !
                          • 10. Re: My file is not saving
                            807603
                            BalusC wrote:
                            Call an OutputStream or Writer on it.

                            Only
                            void doSomething() {
                            new File("c:/foo.txt");
                            }
                            won't create the actual file. It just creates a file location hook.
                            Ok

                            I am doing it this way

                            URL searchEngine = new URL("http://www.yahoo.com/");

                            URLConnection yc = searchEngine.openConnection();
                            yc.setRequestProperty("User-agent","Mozilla/4.0");
                            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
                            String inputLine;
                            String lastStr = "";
                            Writer output = null;

                            while ((inputLine = in.readLine()) != null)
                            +{+
                            lastStr =inputLine;+
                            +}+
                            File file = new File("c:/write.txt");+
                            output = new BufferedWriter(new FileWriter(file));+

                            output.write(lastStr);
                            in.close();
                            output.close();

                            Now I want to save the string(lastStr) contents in a file on the web server.

                            What should I do next??
                            • 11. Re: My file is not saving
                              807603
                              • 12. Re: My file is not saving
                                807603
                                BalusC wrote:
                                Reading the 1st hit of this query: http://www.google.com/search?q=java+io+tutorial+site:sun.com
                                Guys!

                                Why are you all "beating about the bush"?. Zoram, its as simple as you can do. Don't depend on them.

                                Check this code. It will help you
                                URL searchEngine = new URL("http://www.yahoo.com/");
                                
                                URLConnection yc = searchEngine.openConnection();
                                yc.setRequestProperty("User-agent","Mozilla/4.0");
                                BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
                                String inputLine;
                                String lastStr = "";
                                Writer output = null;
                                
                                while ((inputLine = in.readLine()) != null)
                                {
                                +lastStr =inputLine;
                                }
                                File file = new File(realPath +"/write.txt");
                                output = new BufferedWriter(new FileWriter(file));
                                
                                output.write(lastStr);
                                in.close();
                                output.close(); 
                                ofcourse use this: String s =config.getServletContext().getRealPath("WEB-INF");

                                And do check the WEB-INF folder on the server.
                                Best of luck.

                                "GOD HELP THOSE WHO HELP THEMSELVES"
                                "People In This Forum Think Themselves God"
                                • 13. Re: My file is not saving
                                  807603
                                  >
                                  "GOD HELP THOSE WHO HELP THEMSELVES"
                                  "People In This Forum Think Themselves God"
                                  :(...


                                  By the way , I didn't see what error it is giving...