Writing to a file from an EJB

Discussion in 'Java' started by stevesuts@gmail.com, Apr 18, 2005.

  1. Guest

    I am trying to write to a file using an EJB. I need to put the file on
    the server for an FTP process. Does anyone have some example code that
    will do that? Here is what I have so far, but I am having trouble...
    public int writeToFile(String aosData) {
    int returnCde = 0;

    try
    {
    String dirName = ("c:\\AOSFTP");
    String aosFileName = ("aosFtpFile.txt");
    File aosFileDir = new File(dirName);
    FileWriter fileWriter = new FileWriter(aosFileName);
    BufferedWriter buffWriter = new BufferedWriter(fileWriter);


    if(!aosFileDir.exists())
    {
    aosFileDir.mkdir();
    }
    else if(!aosFileDir.isDirectory())
    {
    System.out.println("The Directory does not exist");
    returnCde = 1;
    }
    File aosFile = new File(dirName, aosFileName);
    aosFile.createNewFile();

    buffWriter.write(aosData);


    buffWriter.flush();
    buffWriter.close();



    }
    catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    return (returnCde);
    }

    Any help is greatly appreciated.
    , Apr 18, 2005
    #1
    1. Advertising

  2. kaeli Guest

    In article <>,
    enlightened us with...
    > I am trying to write to a file using an EJB. I need to put the file on
    > the server for an FTP process. Does anyone have some example code that
    > will do that? Here is what I have so far, but I am having trouble...


    What trouble?

    Probably a permissions thing. What perms does the directory have?
    This process runs as the web server, more than likely, and problems are often
    the result of 755 perms or the like (only the owner has write perms).

    --
    --
    ~kaeli~
    A bicycle can't stand on its own because it is two tired.
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
    kaeli, Apr 18, 2005
    #2
    1. Advertising

  3. Guest

    Well, my problem is, there is nothing being written to the file. The
    file gets created in the specified directory, but the string that I am
    trying to write doesn't show up in the file. So, my permissions, I
    think, are ok based on the fact that I can create the file. Can you
    see what is wrong with my write statement. I don't recieve an error on
    the write statement intself, so it thinks that it is writing to that
    file. No errors that I can see.
    , Apr 18, 2005
    #3
  4. Using File I/O in EJBs is a very, very, very bad idea!

    Plus, app server containers adhere to the following permissions

    Table 23 Java 2 Platform Security Policy for a Standard EJB Container
    Permission name EJB Container policy
    java.security.AllPermission deny
    java.awt.AWTPermission deny
    java.io.FilePermission deny
    java.net.NetPermission deny
    java.util.PropertyPermission grant “read”, “*”
    deny all other
    java.lang.reflect.ReflectPermission deny
    java.lang.RuntimePermission grant “queuePrintJob”,
    deny all other
    java.lang.SecurityPermission deny
    java.io.SerializablePermission deny
    java.net.SocketPermission grant “connect”, “*” [Note A],
    deny all other
    Notes:
    [A] This permission is necessary, for example, to allow enterprise beans
    to use the client functionality of the
    Java IDL and RMI-IIOP packages that are part of the Java 2 platform.


    You would need to create a tiered system to have this run effectively


    Standard Java App ---> Session Bean ----> Entity Beans -----> RDBMS

    and in the standard java app is where you do your file io, but not in
    any EJB.




    wrote:
    > I am trying to write to a file using an EJB. I need to put the file on
    > the server for an FTP process. Does anyone have some example code that
    > will do that? Here is what I have so far, but I am having trouble...
    > public int writeToFile(String aosData) {
    > int returnCde = 0;
    >
    > try
    > {
    > String dirName = ("c:\\AOSFTP");
    > String aosFileName = ("aosFtpFile.txt");
    > File aosFileDir = new File(dirName);
    > FileWriter fileWriter = new FileWriter(aosFileName);
    > BufferedWriter buffWriter = new BufferedWriter(fileWriter);
    >
    >
    > if(!aosFileDir.exists())
    > {
    > aosFileDir.mkdir();
    > }
    > else if(!aosFileDir.isDirectory())
    > {
    > System.out.println("The Directory does not exist");
    > returnCde = 1;
    > }
    > File aosFile = new File(dirName, aosFileName);
    > aosFile.createNewFile();
    >
    > buffWriter.write(aosData);
    >
    >
    > buffWriter.flush();
    > buffWriter.close();
    >
    >
    >
    > }
    > catch (IOException e)
    > {
    > // TODO Auto-generated catch block
    > e.printStackTrace();
    > }
    >
    > return (returnCde);
    > }
    >
    > Any help is greatly appreciated.
    >
    Abrasive Sponge, Apr 18, 2005
    #4
  5. Guest

    I have read that you could use file i/o from within an ejb, so I
    thought that I would try. Are you absolutely positive that you can't
    do this, or are you theorizing? Sorry for questioning that, but I read
    what I read on an IBM WSAD website, saying absolutely you can do that.
    I am very new to all of this and I don't have a clue either way......
    , Apr 18, 2005
    #5
  6. wrote:
    > I have read that you could use file i/o from within an ejb, so I
    > thought that I would try. Are you absolutely positive that you can't
    > do this, or are you theorizing? Sorry for questioning that, but I read
    > what I read on an IBM WSAD website, saying absolutely you can do that.
    > I am very new to all of this and I don't have a clue either way......
    >

    Can I take a look at that link?
    Abrasive Sponge, Apr 18, 2005
    #6
  7. wrote:
    > I have read that you could use file i/o from within an ejb, so I
    > thought that I would try. Are you absolutely positive that you can't
    > do this, or are you theorizing? Sorry for questioning that, but I read
    > what I read on an IBM WSAD website, saying absolutely you can do that.
    > I am very new to all of this and I don't have a clue either way......
    >

    Plus I got that permission table from the EJB-Spec itsself.
    Abrasive Sponge, Apr 18, 2005
    #7
  8. kaeli Guest

    In article <>,
    enlightened us with...
    > public int writeToFile(String aosData) {


    Do a system.err.println (or system.out -- whatever, just get a look at it) of
    aosData to make sure it is not just an empty string and the problem is
    actually here or in the process calling this method.

    --
    --
    ~kaeli~
    He's your God, they're your rules - you burn in Hell.
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
    kaeli, Apr 18, 2005
    #8
  9. kaeli Guest

    In article <>,
    enlightened us with...
    > Using File I/O in EJBs is a very, very, very bad idea!
    >
    > Plus, app server containers adhere to the following permissions


    Ooh, this is good to know.
    I make my own beans, but I'm sure someday I'll need to use or at least know
    about this aspect of EJBs.

    I didn't realize they were so different from "normal" beans.

    --
    --
    ~kaeli~
    He's your God, they're your rules - you burn in Hell.
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
    kaeli, Apr 18, 2005
    #9
  10. gentuser

    Joined:
    Jul 8, 2011
    Messages:
    1
    public static boolean writeToFile(String fileName, String dataLine,
    boolean isAppendMode, boolean isNewLine) {
    boolean returnCde=false;

    DataOutputStream dos;
    if (isNewLine) {
    dataLine = "\n" + dataLine;
    }
    try {
    File outFile = new File(fileName);
    if (isAppendMode) {
    dos = new DataOutputStream(new FileOutputStream(fileName, true));
    } else {
    dos = new DataOutputStream(new FileOutputStream(outFile));
    }
    dos.writeBytes(dataLine);
    dos.close();
    returnCde=true;
    } catch (FileNotFoundException ex) {
    returnCde=false;
    } catch (IOException ex) {
    returnCde=false;
    }
    System.out.println("Got a file to write in: "+fileName+" with data:\n"+dataLine+"\n exiting with code:"+returnCde);
    return (returnCde);

    }
    gentuser, Jul 8, 2011
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    1
    Views:
    692
    John C. Bollinger
    Jul 15, 2003
  2. Mumbai Joe
    Replies:
    0
    Views:
    598
    Mumbai Joe
    Jul 29, 2003
  3. Jeremy Haile

    To EJB or not to EJB

    Jeremy Haile, Sep 3, 2003, in forum: Java
    Replies:
    5
    Views:
    535
    Christopher Blunck
    Sep 10, 2003
  4. Sriv Chakravarthy
    Replies:
    0
    Views:
    794
    Sriv Chakravarthy
    Sep 18, 2003
  5. Raymond Schanks
    Replies:
    0
    Views:
    652
    Raymond Schanks
    Aug 3, 2010
Loading...

Share This Page