Read contents of file

hello,
i want to read the contents of an image file without really opening it. i know this can be achieved by reading the header and footer of the file. this header and footer contain details regarding the contents of the image file. with this i can know the pixel arrangement of the image. how can i acieve this?
asrar

You could use a RandomAccessFile object if you know the size in bytes of the header and footer. For the header you would read the first X number of bytes where X is the length of the header. For the footer you'd use the "seek" method to seek to the L - Y point in the file where L is the length of the file obtained using File.length() and Y is the length of the footer. Then read the remainder of the file.
You could also use the new Java NIO package to read the various parts of the image file directly into byte buffers. You'd create two ByteBuffers big enough to hold the header and footer and a third that contains the rest of the file. Check out the java.nio.channels.FileChannel class.
But the guy who posted before is right in that you have to open the file if the header and footer are part of the file.

Similar Messages

  • Read contents of file into outputstream

    Can anyone suggest that what are the best methods to read contents of a file (better cater to both conditions: big file size and small file size) into outputstream and send through socket...
    Thanks.

    Thanks for the answer. But I would like to ask the following question:
    I have a VB application which generates a file. I have a Java application which read the contents of the file.
    is it possible that VB side calls the read() (file) and send() (through socket to destination) methods in Java application once the file is generated? Actually my objective is VB is responsible for generating a file for Java application to read data from and then send the data (not file) to destination....If it is impossible to achieve, any alternative to achieve this?
    Thanks
    Edited by: whkhoo on Jun 15, 2008 8:45 PM

  • Read content of file which is ZIPed

    I need to read contents of a file which is present in ZIP file.
    The zip file is present on online server. I am able to get the Zip Input stream for the Zip file and also able to retrive the file names which are present but when I am trying to retrive the content of file is get a String which has only the file names of the zip. Please suggest what to do.
    Following is my Code.
    // aConn.getInputStream() Gives me access to Zip file present on online Server
    ZipInputStream fileIn = new ZipInputStream(aConn.getInputStream());
                 anEntry = inputStream.getNextEntry();
                 if (anEntry != null) {
                      String filename = arEntry.getFilename();
                                            byte[] b = new byte[2024];
                      fileIn.read(b);
                                 sb.append(b.toString());
                 }

    Reading an input stream into an array of bytes and then calling toString() on that byte array isn't going to give you the contents of the input stream meaningfully. For one thing it might not even be textual data. And secondly that's not how you get textual data.
    A better approach would probably be to get the number of bytes of the given entry from the ZipEntry.getSize method, create a byte array of that size, load only that many bytes, and then (if you're sure it's text data) send the byte array to a String constructor. Or you could use a java.io.ByteArrayOutputStream for that. Note that you'll need to know the character encoding for this.

  • How to read contents of files that do not fall under public security group?

    Hi,
    I need to read the contents of a WCM based xml file that does not fall under public security.
    The process is like this:
    First the user makes chnages to the content.
    The workflow will be triggred based on the security group metadata that is associated with the content.
    Once the content is finally approved our workflow calls a custom idoc script.
    First we tried directly reading the xml contents from the idoc script which was still in the context of workflow. But since content item is still in workflow I was not able to read the changes. So I created a separate content publisher thread and read the DOC_INFO and checked for the dStatus value. If the value is RELEASED then I reading contents by calling ssIncludeXml idoc script.
    This was working fine for public content. But now the requirement is that all content cannot be public. Content authors should not be able to edit the content that does not belong to their group, So we created security groups (and roles) and are associating that groups to the relavent content.
    Beacuse of this change I am not not able to read the non public content. The call to DOC_INFO_BY_NAME service, which gives all the content files' metadata, is expecting the user to be logged in to give the details.
    I tried calling the CHECKIN service with sysadmin and captured the cookies returned by that service and use cookies for the DOC_INFO_BY_NAME service call. But the service call was faling. It is throing the 401 forbidden error with the message that user needs to be logged in to get the details.
    How to address this problem. Someone please help.
    Note: I also tried using ridc for this. I was able to get it working but since it is executing in the context of server ridc api is changing server's environment properties like HTTP_HOST, HTTP_CGIPATHROOT etc. It also seemed like system was becoming non functional after using ridc. When I called check-in the system metadata values like security group are no more loading. Not sure if ridc is the culprit here but worried that it might be causing this issue.
    Regards,
    Pratap

    Sorry, I posted too much details while posting this question. I was saying "not able to read *non* public content".
    Anyway, I was able to resolve the issue. I was able to authenticate with sysadmin credentials in the request to service using basic authentication and was able to read doc info with that credential.
    But I realized there is more than option for reading secure content.
    - I could set user name as sysadmin in the m_environment (if I am in the context of a service) and the call the DOC_INFO_BY_NAME service.
    - I can post an HTTP request to DOC_INFO_BY_NAME service with sysadmin credentials and do basic authorization via the connection. (This is what i have done successfully as of now )
    - I could add guest role to all security groups with R (read) privileges.
    I will look into all options and implement the one which is more apt.
    Regards,
    Pratap

  • Reading Contents of File and displaying output

    I'm trying to read the contents of a file, then loop through the contents, perform some formatting, and display the output. Should I read the file into an array? What is the best approach? Thanks in advance.

    Depends. Is it a text file? Can you format each line independent of others? If so, you can just read one line at a time (BufferedReader) and pass it to wherever the display is. If you need to work on the file as a whole, then you need read it all at once.

  • Read contents of file into outputstream and send through socket

    I have a file. Instead of transferring the whole file through socket to the destination, I will read the contents from the file (big or small file size) into outputstream and send them to the destination where the client will receive the data and directly display it....
    Can you suggest any efficient way/methods to achieve that?
    Thanks.

    I don' t understand what you think the difference is between those two techniques, but:
    int count;
    byte[] buffer = new byte[16384];
    while ((count = in.read(buffer)) > 0)
      out.write(buffer, 0, count);
    out.close();
    in.close();

  • How to print the content in Reverse which is read from the file

    i have written a program to display the content of the file using files concept. now the task is how to display it in reverse order i.e from last to first which is read from the file.

    If it will all fit in memory read the file as you are doing now appending everything into a StringBuilder. Then use the reverse() method of that class to obtain your output.
    http://java.sun.com/javase/6/docs/api/java/lang/StringBuilder.html#reverse()

  • Reading a properties file in a servlet and passing its contents to a JSP pa

    Hi all,
    I'm totally new to Servlet pgmg n JSP. Please can ne1 tell how to read a simple properties file (in a servlet) and pass its contents to a JSP page.Actually the reason is to fill a combo box in a JSP page with the contents of a properties file.If calling a servlet for the same is d best way to do that, plz can ne1 tell me :
    1> whether to override the doPost method in the servlet in which to read d file using FileInputStream
    2> Putting the contents in an array and then how to pass this array to the JSP page.
    Thanks,
    deepthy.

    I'm using a properties file to let my web application know what the name of the database is. I'm using an abstract class GeneralDao which will be extended by all the DAO's (Data Access Objects or java classes containing SQL statements).
    Here's the code :
    protected Connection getDatabaseConnection()
              throws Melding
         Connection dbconn = null;     
         ResourceBundle resBundle;
         try
         Class.forName("com.mysql.jdbc.Driver").newInstance();
         resBundle = ResourceBundle.getBundle("gcoConfig");
         String dbConnectie = resBundle.getString("databaseconnection");
         gcoLogger.debug("lezen databaseconnection in resourceBundle " );
         dbconn = DriverManager.getConnection(dbConnectie);
         } catch (InstantiationException exc)The ResourceBundle is used to read the properties file, named gcoConfig.properties.
    With the getString() command I read the content of the string named databaseconnection.
    The gcoConfig.properties file is placed inside the folder C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\gco\WEB-INF\classes
    The GeneralDao is placed in the folder C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\gco\WEB-INF\classes\org\gertcuppens\cluifDao.
    So this class is placed some folder below the WEB-INF\classes folder of gcoConfig.properties.
    And the content of the properties file is just one line :
    databaseconnection=jdbc:mysql://localhost/cluif
    If you want to let the JSP know what's inside the properties file, wrap the content in a Javabean, put it in the request or even the session and pass the control to the JSP.

  • Unzip File and Read Contents

    Hi,
    I want to unzip a file(called outer.zip) present on the Presentation server and read inner.zip file which lies within the outer.zip.
    i wrote the following program, but i am getting an error in the last step .Can some one tell me themistake i am committing here:
    TYPES : BEGIN OF sda_content,
      lv_buf(65535) type x,
      END OF sda_content .
    DATA : ls_sda_content type sda_content ,
            lt_sda_content type table OF sda_content  .
    DATA : lv_file_length type int4 .
    DATA : x_buf TYPE xstring .
    CALL FUNCTION 'GUI_UPLOAD'
      EXPORTING
        filename                      = 'C:\outer.zip'
       FILETYPE                      = 'BIN'
    IMPORTING
       FILELENGTH                    = lv_file_length
      tables
        data_tab                      = lt_sda_content
    CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
      EXPORTING
        input_length       = lv_file_length
    IMPORTING
       BUFFER             = x_buf
      tables
        binary_tab         = lt_sda_content
    clear : lt_sda_content ,lv_file_length,x_buf .
    data : cl_inst type ref to cl_abap_zip .
    CREATE OBJECT cl_inst.
    CALL METHOD cl_inst->load
       EXPORTING
         zip             = x_buf.
    CALL METHOD cl_inst->get
      EXPORTING
        NAME                    = 'inner.zip'
      IMPORTING
        CONTENT                 = x_buf.
    Thanks and Regards,
    Gaurav

    Hi Gaurav Rathi  ,
    Even am facing some problem .I cannot open the ZIP file? Can you please tell me where exactly you have corrected your mistake?
    Thanks
    Dan

  • If I use an app to remove duplicate pix does the app  read content  or only  the file name such as IMG

    If I use an app to remove duplicate pix does the app  read content  or only  the file name such as IMG. I have some 2500 pix  and the thought of trawling thru  them  toremove duplicates is mind numbing.
    If the apps  read the content then I am ok about that  but if they  only read the  file name ... they maybe  deleting stuff that  isnt really a duplicate

    You have to contact with the developer of the application. We do not know which app you are referring to and we do not know how that application works, but all apps of that type are supposed to delete duplicate files with the same format (like IMG, JPG...)

  • Read from .txt file and output the content as two arrays

    I am using the contoured move to control the x-y stage. The trajectory datas for x and y axis are generated using my interpolation program and it is stored in a .txt file as two columns. What I want to do is read .txt file and output the content of this file as two arrays. Is there anyone has any ideas? Thanks, the .txt file is attached.
    Attachments:
    R.75.txt ‏172 KB

    Hi Awen,
    This is quite easy to do, you can merely use the "read from spreadsheet file" function to get a 2D array (2 columns and n rows) and then use the index array function to get whatever row/colums you want..
    Hope the attached VI helps you
    When my feet touch the ground each morning the devil thinks "bloody hell... He's up again!"
    Attachments:
    read sprdsheet file.vi ‏27 KB

  • Can a servlet read a jsp file and display its contents?

    Hi,
    I would like to know if Servlets can read a Jsp file and display its contents.. Right now for our website, I am using a html file to be displayed after a successful post operation through the Servlets...
    -Thanks!

    I posted this in another thread.
    Here's some code that reads using a URL. This doesn't work with JSPs as the server executes JSP when it connects but it's useful for other file types. Note that the filename is a URI</h1>This is <code>show.jsp</code></h1>
    <hr>
    <%! String file = null; %>
    <%! java.io.InputStream is = null; %>
    <%
    file = request.getParameterValues("filename")[0];
    try {
       is = (new java.net.URL(file)).openStream();
    } catch (java.io.IOException ioe) { out.write(file + " not found!<br>"); }
    java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(is));
    String line = "";
    %>
    <pre>
    <%
    while((line = in.readLine()) != null) {
    %>
    <%=line%>
    <%
    %>
    </pre>

  • FTP to Read content of Text/xml file

    Hi,
    I need a help for reading content of text/xml file through FTP. Below I just am explaining the scenario.
    Our application server is in UNIX. Now we have to run a report program to access in to a FTP server which is in windows platform. Using FTP_CONNECT, FTP_COMMAND, FTP_DISCONNECT we are able to connect to FTP server and also able to copy files from FTP server to SAP application server. After copied in application server, we are able to read the content of the txt file in to internal table in ABAP program using OPEN DATASET. But our requirement is that we want to read the text or xml file content into internal table while accessing into FTP server from SAP application instead of after copying the file into application server.
    So please help me to solve my problem.
    -Pk

    Thank you Bala,
    But can you help me what should I pass against FNAME and CHARACTER_MODE Import parameter? Should I pass the full path of the file with name or only I have to pass file name ? For example if my text file name in FTP Server is test.txt and the IP of the ftp server is 10.10.2.3 then should I pass the value against FNAME as '
    10.10.2.3\xyz\text.txt' ? Here xyz is the name of the directory in C drive where test.txt is exist.
    Please help me.
    -pk

  • How to read a PDF file content???

    Hi Experts,
    I need to read the pdf file content.
    Pdf file is in some repository
    I m unable to read pdf data with getContent() function.
    Please suggest me a way to read the pdf file
    Help will be appreciated and rewarded

    Hi Pankaj,
    Are you able to achieve the above said functionality? Even I too have the similar requirement.
    Can you pls let me know the solution or alternatives for your requirement you have followed...
    Thanks in advance.
    Nandu.

  • Read contents inside pdf file programmatically in SharePoint

    I have a SharePoint document library, My Requirement is when user add PDF file on the document library the event receiver fire and read contents inside
    pdf file programmatically. After the start workflow according to the result of event receiver.

    If your question is about handling events in apps for SharePoint, see these links:
    http://msdn.microsoft.com/en-us/library/office/jj220048%28v=office.15%29.aspx
    http://msdn.microsoft.com/en-us/library/office/jj220051%28v=office.15%29.aspx
    If what you need is a way to extract text from the PDF inside the event handler, see this example that uses leadtools.
    http://support.leadtools.com/CS/forums/ShowPost.aspx?PostID=43894
    You should use PDF text extractor in your Event Handler code -
    You can use iTextSharp for reading content
    http://www.codeproject.com/Tips/387327/Convert-PDF-file-content-into-string-using-Csharp

Maybe you are looking for