Results 1 to 11 of 11
  1. #1
    thevoice is offline Member
    Join Date
    May 2008
    Posts
    8
    Rep Power
    0

    Default How to create file on server?

    Hello there,
    I have one ruby web application in which i use java program to create file on linux server.
    The program got executed successfully but file is not created in folder that i specify.

    Following is my java code that i used to create file on server:

    import java.io.*;
    import java.lang.*;
    public class Test
    {
    public Test()
    {}
    public static void main(String [] args) throws Exception
    {
    FileOutputStream fos;
    DataOutputStream dos;
    try
    {
    System.out.println("start here");
    File file= new File("/sites/myproject/reports/Test.txt");
    fos = new FileOutputStream(file);
    dos=new DataOutputStream(fos);
    dos.writeInt(2333);
    dos.writeChars("Hello");
    System.out.println("end here");
    }
    catch(IOException eio)
    {
    eio.printStackTrace();
    }
    }
    }
    In above code "/sites/myproject/reports/Test.txt" is file path on linux server.

    Can anybody help me in solving my problem?
    Is there any settings on linux server to create file?

    For more details..
    java program is called from ruby web application which runs on linux server and i am using java program to create file on the same linux server.

    Regards,
    TheVoice.
    Please help and guide to solve my problem.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    thevoice is offline Member
    Join Date
    May 2008
    Posts
    8
    Rep Power
    0

    Default How to create file on server?

    Hi Eranga,
    Thanks for reply.
    No,i dn't get any exception.
    Program executes perfectly but file is not created on server.

    Regrads,
    TheVoice.

  4. #4
    daGame is offline Member
    Join Date
    May 2008
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by thevoice View Post
    Hi Eranga,
    Thanks for reply.
    No,i dn't get any exception.
    Program executes perfectly but file is not created on server.

    Regrads,
    TheVoice.
    I dont know any thing about linux/unix but try putting the root folder in here
    File file= new File("C:/SW/Test.txt");
    Ignore this if linux doesnt work that way ;)

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    It doesn't matter actually. Your code is fine, so the problem is the path. That your code can't create full path you given. I mean can't create folder structure. What only can do is, if the file is not exist, it created there. So check your path.

    But why I'm worried is that, if the path is wrong you should get an exception.

  6. #6
    mox8iro is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    beware of gaps....

    linux has a special treatment of gaps.. maybe at the end there is gap... (space character)

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    As I said in my previous post, I'm not a Linux user. But in File object, if you have a space character at the end of the path, VM neglect that. Both of the following are same.

    Java Code:
    File ff = new File("c:/aa.txt");
    
    File ff = new File("c:/aa.txt ");
    But if you have an space character at the start you'll get the FileNotFoundException.

  8. #8
    mox8iro is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    i found this post by having the same problem... and it was solved by accident... and realized that the problem was the space at the end... linux does not treat space character as a special character and leaves it as is.. actually it will try to find the path with the space..
    regards.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    In Java too, the last space is not treated as a special character. Simply trim that last space if any.

  10. #10
    manishmoney is offline Member
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Smile

    System.out.println("start here");
    File file= new File("/sites/myproject/reports/Test.txt");
    Insert one more line here,

    if(!file.exists()){
    file.createNewFile();


    This will work. Even this does not work check for permission of folders. You should have write permission for "reports" folder.

    Good Luck

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. How to create a file
    By Alpha in forum New To Java
    Replies: 7
    Last Post: 01-06-2012, 08:10 PM
  2. Replies: 2
    Last Post: 08-14-2008, 09:33 PM
  3. How to create a Quote Server in Java
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 09:16 PM
  4. Automation server can't create object
    By katie in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-06-2007, 11:53 PM
  5. Replies: 1
    Last Post: 07-24-2007, 01:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •