1 2 Previous Next 16 Replies Latest reply: Jun 17, 2007 8:31 PM by EJP RSS

    problem in writing data in file......

    807605
      i am having problem in passing a String object path ;
      taken from user in following way

      FileWriter fw = new FileWriter(path,append);

      error is : (The filename, directory name, or volume label syntax is incorrect)
      but if i am using

      FileWriter fw = new FileWriter("f.txt",append);

      it works...

      but i want filename from user... how to achieve this
        • 1. Re: problem in writing data in file......
          807605
          So you pass wrong value in path variable.
          • 2. Re: problem in writing data in file......
            807605
            thanks but....
            i have tried this way also

            String path = "E:/fname.txt";
            FileWriter fw = new FileWriter(path,append);
            wont work....                                        


            but if this way ...
            FileWriter fw = new FileWriter("E:/fname.txt",append);

            it works....
                      whats happening...
            • 3. Re: problem in writing data in file......
              807605
              Are you sure you use "e:/file" not "e:\file"?
              • 4. Re: problem in writing data in file......
                807605
                yes sure......
                • 5. Re: problem in writing data in file......
                  807605
                  Really really?
                  Because "e:/file.txt" works fine while "e:\file.txt" throws exception you post: The file name, directory name, or volume label syntax is incorrect)
                  :)
                  • 6. Re: problem in writing data in file......
                    807605
                    could u please give simple example where a user inputs the name of the file
                    then he enters the data and the data is entered in a file having name given by the user...
                    not predefined in the program
                    • 7. Re: problem in writing data in file......
                      807605
                      You already have such code as I understand your question.
                      • 8. Re: problem in writing data in file......
                        807605
                        i need one which works
                        mine is not working .....
                        pls help
                        • 9. Re: problem in writing data in file......
                          807605
                          Show you code we'll find problem :)
                          • 10. Re: problem in writing data in file......
                            807605
                            class writeData
                            {
                                 public String getName()
                                 {
                                                String name=null;
                                 try
                                 {
                                      char c;

                                      StringBuffer bf=new StringBuffer();
                                      System.out.print("Enter the File Name:- ");
                                      while((c=(char)System.in.read())!='\n')
                                      {
                                           bf.append(c);
                                      }
                                      name=bf.toString();
                                           
                                 }

                                      catch(Exception e)
                                      {
                                      System.out.println("Error is:-"+e);
                                 }     
                                      return name;
                                 }

                            public void wfile(String name1)
                                 {
                                      
                                      
                                      try
                                      {
                                      
                                      
                                      
                                      String path ="D:/fname.txt";
                                      boolean append = true;
                                      FileWriter fw=new FileWriter(path); // its not working....
                                      //FileWriter fw=new FileWriter("D:/fname.txt");
                                      }
                                      catch(Exception e)
                                      {
                                           System.out.println("Error is :- "+e);
                                      }

                                 }
                            }
                            public class bravo
                            {
                                 public static void main(String args[])
                                 {
                                      writeData wd = new writeData();
                                      String n = wd.getName();
                                      wd.wfile(n);
                            }
                            }
                            • 11. Re: problem in writing data in file......
                              802908
                              Hmm

                              i have in some of my properties Files some paths which look like this:
                              C:\\Program Files\\Some Program\\Some File.txt
                              with double (!) backslashes.
                              I guess the problem is related to the slash.
                              What is the outcome if you use \\ ?

                              regards

                              BugBunny
                              • 12. Re: problem in writing data in file......
                                807605
                                Please use code tags.
                                Anyway your code works fine for me in this version.

                                But yes, you have problem with name1 (not with path): while reading file name you also read terminating '\r', so entering abc.txt you are trying to create abc.txt\r actually - this name is wrong. You can correct your code changing '\n' to '\r' - this is fast but wrong way. It's better to use another method of reading.
                                • 13. Explain the code....
                                  807605
                                  thanks.....
                                  FileInputStream fstream = new FileInputStream("Emp.txt");
                                                 DataInputStream dstream = new DataInputStream(fstream);
                                                 BufferedReader bf = new BufferedReader(new InputStreamReader(dstream));
                                                 String data = null;
                                                 String comma = ",";
                                                 
                                                 
                                                 while((data = bf.readLine()) != null)
                                  • 14. Re: Explain the code....
                                    EJP
                                    The DataInputStream in this stack is unnecessary.
                                    1 2 Previous Next