3 Replies Latest reply on Nov 29, 2002 3:52 PM by dddu88

    Writing a file in a EJB

    dnloreto

      Hello!

      I've written an EJB that reads a file on the filesystem using the FSContext from the JNDI. It returns the file content as a string (it's an ASCII file) to the client.

      Now I'm trying to write a file to the filesystem and I'm unable to. I've looked around for documentation on how to do it, but I can't find any. I looked at the fscontext.jar contents but the methods that look revelant are all protected.

      Can anyone give me a piece of code where a Java object or even a simple piece of text is writen on a file? Or correct my piece of code...

      Thank you very much!

      HappyGuy

      public void writeFile(java.lang.String sFilePath, java.lang.String sFileName, java.lang.String sFileContent) {
      try
      {
      Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
      env.put(Context.PROVIDER_URL, "file:///" + sFilePath);
      Context initialContext = new InitialContext(env);
      System.out.println("Environment = " + env);

      File fNewFile = new File(sFileName);
      System.out.println("Created File");

      (new ObjectOutputStream(new FileOutputStream(fNewFile))).writeObject(sFileContent);
      System.out.println("Wrote File");

      initialContext.createContext(fNewFile);
      System.out.println("Created File in FileSystem");
      }
      catch(Exception e){
      System.out.println("Exception! Error: " + e.getMessage());
      }

        • 1. Re: Writing a file in a EJB
          regulus

          I am able to read and write file in a EJB by using
          javax.activation.FileDataSource.
          Something like this:

          FileDataSource ds = new FileDataSource("/tmp/abc");
          InputStream is = ds.getInputStream();
          OutputStream os = ds.getOutputStream();

          The EJB spec discourages the use of file io, so I'm not sure if this is legal or it's a hole in JBoss, and will be made illegal in future JBoss versions... I'm using JBoss 2.4.4. Can somebody from JBoss team help clarifying this? Thanks in advance.

          • 2. Re: Writing a file in a EJB
            corban

            Hi,

            afaik the ejb spec does permit direct file access via the java.io package, you would "leave" the container through an input/output stream. but javax.activation.FileDataSource is a resource provided by the container, i think you can use it as you can use a DataSource for example.

            • 3. Re: Writing a file in a EJB
              dddu88

              Hi, can we use FileDataSource in activation package for extensive file IO to local drive instead of using entity bean to put into database? because our requirements requires that.

              Thanks