Extract certain bytes number of bytes from file

If I wanted to extract a certain number of bytes from a DataInputStream what would the best way to stop the copy after a certain number of bytes?
would the use of the written field of the stream do?
has anyone got any suggestions?
Cheers :)

is this a trick question or would this do what you want?
Here's the javadoc from the "read" method of the DataInputStream Class:
int read(byte[] b, int off, int len)
Reads up to len bytes of data from the from the contained input stream into an array of bytes.

Similar Messages

  • Delete X number of lines from files

    I am trying to find a way (either through an extension or
    some other third-party freeware) to remove a number of lines from
    the beginning of my files and replace them with alternate code.
    I know that in many cases, a Search and Replace would do the
    trick. But in my case, the lines that I want replaced do not
    exactly match. For example, I might want to remove the first 15
    lines from the beginning of each file in question; however, I might
    have three lines of code in the files that each set a variable
    value, with that value being different in each file. Thus, as far
    as I understand it, the DW Search and Replace would not work for
    me.
    Even if I have can just find some way to first strip the
    lines of code out without replacing it with the new code I want,
    that would be fine. Adding in the new code afterwards would not be
    an issue.
    Any help would be very much appreciated. Thank you very much.
    James

    On Mon, 7 May 2007 17:14:52 +0000 (UTC), "MUSC Webmaster"
    <[email protected]> wrote:
    > Gary: Thanks for the suggestion; however it wasn't quite
    working for me.
    >After your posting, I played around for a bit with
    regular expressions and the
    >best that I could do was get an expression (^.*\r) that
    grabbed all of the
    >first line, with the "r" representing a carriage return
    as opposed to a line
    >feed. If I try to expand on that expression, the search
    results come up with
    >nothing.
    Actually the linfeed is the \n in my suggestion. The ^
    designates the
    beginning of the string.
    Are you on a Mac? I tested it and it worked correctly on my
    windows
    machine. Come to think of it, I have my line feeds set to
    Unix because
    that's what my web server uses. You can try replacing the \n
    with \r or
    \n\r depending on which line feed you're using:
    ^.*\r.*\r.*\r.*\r.*\r.*\r.*\r.*\r.*\r.*\r.*\r.*\r.*\r.*\r.*\r
    or:
    ^.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\n.*\r\ n
    One of the three should work.
    Gary

  • How do I store an Int, a short, and multiple bytes from file in byte array

    I'm attempting to do this for a project but can't figure out how to store the three values in one byte array. This is what i've tried
    public void send(byte[] input)
              // TODO
              ByteArrayInputStream bais = new ByteArrayInputStream(input);
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              DataOutputStream dos = new DataOutputStream(baos);
              byte[] pData = new byte[100];
              try {
                   dos.writeShort(checkSum);
                   dos.writeInt(nextSeqNum);
                   pData=baos.toByteArray();
                   bais.read(pData);
              } catch (IOException e) {
              DatagramPacket dgp = new DatagramPacket(pData, pData.length);When i set pData=baos.toByteArray() it changes the size and then stops me from being able to read the other 94 bytes into the array to send. Any ideas?

    You don't need the ByteArrayInputStream at all.
    dos.writeShort(checkSum);
    dos.writeInt(nextSeqNum);
    dos.write(input);
    pData = baos.toByteArray();
    When i set pData=baos.toByteArray() it changes the sizeNo, it makes it refer to a new byte[] array containing what you've written to the BAOS. That's what it's supposed to do.

  • Error 105, Could not read full block (2048 bytes) from checkpoint file ~/dirchk/sdfsdj.cpe

    Hi expert,
        i am getting below error in goldengate  due to mount point full and i released the space and still the same error for all gg processes. i can see the *cpe cpr file become 0 bytes. so i deleted and re added the extract and repliacat and while adding the replcat i used add replicat  checkpoint table because of that multiple entries of same replicat came in checkpoint table . my checkpoint details also  present in ./GLOBALS. now my doubt is if add  replicate with mentioning checkpoint table name  will duplicate entry will be created or what is the work around for this.
    MANAGER RUNNING
    Invalid checkpoint for EXTRACT  qqqq   (error 105, Could not read full block (2048 bytes) from checkpoint file XXXXXXXXX)
    Invalid checkpoint for EXTRACT  qqq(error 105, Could not read full block (2048 bytes) from checkpoint file XXXXXXXXXX)
    Invalid checkpoint for REPLICAT qqq  (error 105, Could not read full block (2048 bytes) from checkpoint file XXXXXXXXXXX)

    Hi Kariyath
    Increase the page size of your windows machine.Check for the recommanded page size.Remember that the recommanded page size should be the lower limit.If you have any issue feel free to ask.Your prob will be solved
    Award suitable points

  • How to read a file starting from certain line number

    Hi there
    I need to read a large text file line by line. The user will have the ability to stop at any time and run the application to resume reading and processing. For example, if the user stops at line 350, next time when the user run the application, I want him to be able to start with line 351. I am using BufferedReader to read the file. What should I do enable to read the file starting certain line number? Each line in the text file is not equal in length, so I can not control by bytes ...
    Thanks!

    Since you can't count bytes, there's no way of knowing where to jump except actually reading trough all those preceding lines. (You don't have to store them, though.)
    for(int linenum=0, linenum<jumpTo; ++linenum) {
        reader.readLine();
    }

  • How can I extract certain pages from my document and save as another file?

    How can I extract certain pages from my document and save as another file?

    You would need Adobe Acrobat to do that, not Adobe Reader.

  • Extracting certain files from a jar

    I am creating a jar that when d-clicked on will extract its files to the computer the jar resides on. Here is my question: how do I get it to only extract certain files(ie. all files except the classes used to give the extraction command upon d-clicking and the manifest)?
    Thanks!!

    The following post won't solve the doubl clicking problem, but it will get you started on getting specific files from a Jar.
    Go to:
    http://forums.java.sun.com/thread.jsp?forum=22&thread=138514

  • What is the maximum number of 263 byte text files that can fit in a folder?

    what is the maximum number of 263 byte text files that can fit in a folder - on a windows 2000 operating system?
    I created a java program that creates these text files.
    It was set to loop 30,000 times.
    It created a total of 21874 html files before throwing a null exception.
    I tried to copy and paste 10 more html files manually and it said not enough disk space.
    however, I made a New Folder in that same folder and I was able to successfully paste the folders in
    there.
    That's interesting.
    It appears there is a size limit in each folder, BUT I can make another folder within that and successfully paste more html files in there
    Does anyone know the rules governing this feature.
    Stephen

    Whatever the answer is, it has nothing to do with Java programming. Microsoft might know, though, and that's where you should look.

  • How to Print Bytes[] from a resultset in a text file? Thanks a Lot!!

    Hi all I'm having a prb in printing the Byte[] from a Resultset in a textfile . Every time I exceute this program different values is printing in the text file and in binary format like:
    [B@29c3,[B@347a,[B@3f2f,[B@49e6,[B@549d
    and I'm supposed to get the out put as :
    0000690D,00002107,00009F16,00003008,0000ED0F
    Here is the sample code!
    public static void main(String[] args) {
         try {
    Class.forName(drvName);
    Connection con = DriverManager.getConnection(url, SDR_userName, SDR_passWord);
    Statement stmt = con.createStatement();
    int count1 = 0;
    char c = 'c';
    ResultSet rs1 = stmt.executeQuery(_sqlQuery);
    FileOutputStream fos = new FileOutputStream(inputFileName);
    Writer out = new OutputStreamWriter(fos, "UTF8");
    while (rs1.next()) {
    byte[] _id = rs1.getBytes(1);
    String _name = rs1.getString(2);
    out.write("_id=="+id+"==name=="+_name);
    int len = sales_account_id.length;
    System.out.println("Length==" + len);
    String s = new String(_id, 0, len, "UTF8");
    char c1[] = s.toCharArray();
    for (int i = 0; i < s.length(); i++) {
    System.out.print("s1" + c1);
    System.out.println("_Id" + s.toString());
    try {
    for (int i = 0; i < len; i++) {
    c = (char) _id;
    System.out.println("_Id" + c);
    catch (Exception statemets...){}
    Thanks a lot in advance!!

    out.write("_id=="+id+"==name=="+_name);
    in this line you are printing the array _id. when you do that ( print array ) you get the address of that array. so the output is expected.
    what you can do is create a new String from the byte array and write that string to the file.
    String temp = new String( _id );
    out.write("_id=="+temp+"==_name=="+_name);
    hope i got your problem right

  • How to read a byte[] from a file

    Hi can someone help me to read a byte[] from a file. I need to save a byte[] to a file and then retrieve it later can someone help in this..
    Thanx

    there are actually docs on the classes ByteArrayInputStream and ByteArrayOutputStream here:
    http://java.sun.com/javase/6/docs/api/
    go there (copy & paste in browser) and text search for the above classes.
    Then read into it. That way, you will come to a better comprehension of the entire thing. You may as well want to read this:
    http://java.sun.com/docs/books/tutorial/essential/io/index.html
    And for the future: if you have general problems, search API docs and tutorials first and solve them yourself. The guys around here can get pretty annoyed about "Teach me the entire Java Thing" (also called "someone do my homework") requests.
    If you have a special problem, ie an Exception you don?t understand or a method wich you don?t know how it works, everybody will like to help you.
    Only few real serial killers frequent these forums.

  • How can we read some bytes from every line of the file

    How can we read some bytes from the every line of the file moving on to the next line
    without using the read line

    Actualiy readLine() takes more execution time
    for reading a part of line if we can do so without
    readLine() we can save some time...Well, if you knew, beforehand, the length of each line, you could use RandomAccessFile and its seek method, but, since you don't, you would have to read the rest of the line character-by-character, checking to see if it is a newline, in order to place the "cursor" at the beginning of the next line in order to read the next few characters you want.
    So, as you can see, you will need to read the entire line anyway (and if you do it yourself you also have to do the checking yourself considering all three possible end-of-line sequences), so you just as well use readLine().
    Some people may suggest Scanner and it's nextLine() method, but that also needs to read the rest of line (as evidenced by the fact that it returns it), so that is no different than the readLine() (or read it yourself) solution.

  • Cutting first few byte from Large files

    Hi,
    I have a requirement to cut out first 20 bytes from very large binary files (in the order of 1-4 GB).
    I was looking at RandomAccessFile and FileChannel to accomplish this. But still, it seems that there will be a lot of I/O involved as it will be practically copying over the remaining bytes to another file only.
    Is there any other approach which doesn't cause a lot of disk I/O.
    Thanks.

    >
    I have a requirement to cut out first 20 bytes from very large binary files (in the order of 1-4 GB).
    >
    Somehow I suspect your 'requirement' isn't telling us the whole picture. If you can explain what the purpose of that requirement is there might be a couple of options.
    >
    replace the first 20 bytes with values that won't interfere with how the file needs to be used.
    >
    One reason might be becaue the files need to be read and processed and the first 20 bytes will interfere with that processing. Depending on the tool that will read the file you might be able to replace the 1st 20 bytes with NULLS (0 hex), SPACES, or some other set of values that the tool will ignore or silently fail on and continue.
    It would be very fast to replace 20 bytes in each file with some other value.
    Second option could be use a file filter that strips off the first 20 bytes when the file is read. Again this depends on the tool being used to read the file.
    In *nix systems you could have a small filter that reads the files, strips of the first 20 bytes and pipes its output into the tool you are using to read the actual file.
    If Java were the tool doing the reading it is even simpler. Just open the RandomAccessFile and move the file pointer to where you want it before the first read.
    A third option is to modify the process that delivers the files to you to strip of the first 20 bytes. If the files are being delivered by FTP you could easily add a filter to do this.
    Fourth option is to have the file supplier zip up the files for you. You can easily add a filter to the process to strip the 20 bytes as the file is being zipped. If these are files that will be kept and moved around the compression will save space and transmission time and you can sneak a filter in for free.
    Can you tell us how the files will actually be used?

  • Reading in a certain number of characters from a file.

    Hi guys,
    I need some pointers on how to read in a specified number of characters from a text file.For example,how would I read in the first 100 characters to an array from a text file with an unspecfied number of characters (more than 100 rather than less!).
    At present I am getting code errors being thrown due to reading in beyond the array size ie
    java.lang.ArrayIndexOutOfBoundsException: 100.

    post more code , this exception tells me nothing
    without seeing what you are actually doing.
    public String getText (String file)//textfile to be analyzed will be inputted as argument 150407 0356
       //15042007 0612 Check notes on overloaded getText method below.
           int temp;
           inputFile = file;
           int i=0;
                try
                    BufferedReader bGetFile = new BufferedReader (new FileReader(inputFile));
                    while ((temp=bGetFile.read())!=-1)
                        textFromFile[i] = (char)temp;
                        i++;
                    /*for (int j=0;j<MAX;j++)
                        System.out.print(textFromFile[j]);//25042007 0208.For Testing purposes
                    inputText=new String(textFromFile).trim();
                    bGetFile.close();
                catch (IOException e)
                    System.out.println (" ");
                    System.out.println ("Sorry.An error occurred while trying to read from the input file.Unable to proceed!");
                System.out.println ("Processing the input text file "); //25042007 2318 being used for debugging purposes.          
                return inputText;
        }If the file being read is greater than MAX characters it will throw the exception mentioned.What I would like to do is read in MAX characters and discard the rest.

  • Oops. Can I read my byte stream files with missing headers?

    My fault, I know, but I wrote out an 1D array of numbers to a file using the simple Write File vi, not realising that a header would have been a good idea at the time. Now I can't read them back in because LabVIEW doesn't know what they are, and trying to put the original array element (Dbl Precision Number) on the Byte Stream Type of Read File doesn't work.
    So, is there any way that I can insert an appropriate header back into the beginning of the file so that I can then read them in successfully?
    (No, unfortunately I can't regenerate the data. Believe me, I would if I could!)
    Thanks,
    Riggy

    I suppose you used the Write File function from the File I/O pallette since there is no Write File VI?
    If that is the case your file should be exactly 8*N bytes long, if the array had N elements of type DBL.
    You can simply read back you data with the 'Read characters from File' VI (from the File I/O pallette) and then typecast the read string to a 1D array of DBL. (typecast is the first icon in the 'Advanced--Data Manipulation' pallette.
    -Franz

  • How to extract a folder (and its contents) from inside a zip file?

    There is a zip file which contains a folder inside it. The folder itself contains a few files. I would need to know how to extract the folder (with its contents) from inside a zip file.
    I have found a few unzipping code samples which show how to handle a folder inside a zip file. An example is shown below:
    public static void extract(String workingDirectory, byte[] zipFile)
    throws Exception {
    ByteArrayInputStream byteStream = new ByteArrayInputStream(zipFile);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ZipInputStream zipStream = new ZipInputStream(byteStream);
    ZipEntry zipEntry = null;
    String nameZipEntry = null;
    byte[] contentZiphttp://forum.java.sun.com/post!default.jspa?forumID=31#
    Click for code tagsEntry = null;
    boolean isDirectory = false;
    int indexFileSeparator = -1;
    String directory = null;
    String fileName =  null;
    while ( (zipEntry = zipStream.getNextEntry()) != null )
                nameZipEntry = workingDirectory + File.separator + zipEntry.getName();
                isDirectory = zipEntry.isDirectory();
                if (isDirectory) {
    File file = new File(nameZipEntry);
    file.mkdirs();
                else
                    // read zipEntry
                    byte[] buf = new byte[1024];
                    int c = 0;
                    while ( (c = zipStream.read(buf)) != -1)
                        out.write(buf, 0, c);
                    indexFileSeparator = nameZipEntry.lastIndexOf(File.separator);
                    directory = nameZipEntry.substring(0, indexFileSeparator);
                    fileName =  nameZipEntry.substring(indexFileSeparator+1,nameZipEntry.length());
                    FileSystemTools.createFile(directory, fileName, out);
                    out.reset();
                    zipStream.closeEntry();
            zipStream.close();
            byteStream.close();
    }The code sample which deals with the part where the zipEntry is a directory creates a directory with the same path and name. (highlighted in bold)
    Another similar variation is:
    File file = new File(dirDestiny.getAbsolutePath() + File.separator + zipEntry.getName() );
    if(zipEntry.isDirectory())
          file.mkdirs();When the code creates a directory for the folder, does it unzip the contents inside the folder as well?
    If not, how do I extract the files inside the folder?

    Have you already tried to see if the sample code you downloaded works or not? Maybe if you try out the code yourself you can see if it extracts files from a directory within a zip file?
    I like to use pkzip. It is a command line compression/uncompression tool that can be used from a batch file. If you assignment involves unzipping large amount of zip files on a regular basis, I recommend taking a look at pkzip.

Maybe you are looking for

  • Issue when trying to launch Handbrake, problem report generated

    I've posted this over in the Handrake forums but soi far I haven't had a response.  I'm hoping someone here might be able to point me in a direction.  I've suddenly been unable to launch Handbrake.  Every time I try the icon bounce once or twice on t

  • Web cam

    Having no experience of webcams, Apples I-Sight seems way too expensive. Are there any other brands which will work with an E-Mac?

  • Alerts in Oracle 10g

    Please refer me any useful like/tut for creating Oracle Server genrated Alerts in 10g. Regards, Abbasi Edited by: Abbasi on Jul 1, 2010 1:51 PM

  • Mail stuck in OUTGOING MESSAGES on OS X version 10.9.4

    I am running OS X Version 10.9.4 and there are three mails stuck in my outgoing mail, It does not show in the outbox when i access it. It only shows when I am sending mail it says: Outgoing Messages - Sending mail 90 of 93, and the three always stays

  • Compressor long render times

    Is it normal for Compressor 2 to take 12+ hours to render a 45 minute DVD? I'm exporting from FCP 5.0.4 to DVD Best Quality 90 minute 4:3. I started at 1:20 pm, and it said 4 hours. It's now almost 1:20 am, and it still has 45 minutes to go. It seeme