Trying to write out a binary file

I have found that using the Dataoutputstream is the way to read the file but writing I can't seem to get. Below is the code. It is writing out some data but not all is accurate is there anyother way to copy exactly what is in one file to another by just renaming??
DataInputStream brL = new DataInputStream(new FileInputStream(LogFile));
          DataInputStream brE = new DataInputStream(new FileInputStream(ECGFile));
          DataInputStream brS = new DataInputStream(new FileInputStream(SpeechFile));
          DataInputStream brH = new DataInputStream(new FileInputStream(HistoryFile));
DataOutputStream bwL = new DataOutputStream(new FileOutputStream("c:\\Projects\\FirstSupport\\LOG_data.out"));
          DataOutputStream bwE = new DataOutputStream(new FileOutputStream("c:\\Projects\\FirstSupport\\ECG_data.out"));
          DataOutputStream bwS = new DataOutputStream(new FileOutputStream("c:\\Projects\\FirstSupport\\SPEECH_data.out"));
          DataOutputStream bwH = new DataOutputStream(new FileOutputStream("c:\\Projects\\FirstSupport\\HISTORY_data.out"));
          System.out.println("Starting to copy files");
          try{
          int c = -1;
          while((c = brL.read()) != -1){
               bwL.write(c);
          while((c = brE.read()) != -1){
               bwE.write(c);
          while((c = brS.read()) != -1){
               bwS.write(c);
          while((c = brL.read()) != -1){
               bwH.write(c);
     }

use a FileInputStream and a FileOutputStream. You don't need to use the DataInput and OutputStreams. You can use a byte array to store the contents of the file and then write the contents of the byte array to a file.
try{
    FileInputStream f = new FileInputStream("myFile.dat");
    byte[] contents = new byte[f.available()];
    f.read(contents);
    FileOutputStream o = new FileOutputStream("myFile.new");
    o.write(contents);
    f.close();
    o.close();
catch(IOException ioe){
    ioe.printStackTrace();

Similar Messages

  • Problem when streaming out a binary file

    Hi,
    I am trying to stream out a binary file to an output stream (not a file, but a socket). My file is a gzip file, and I was initially simply trying to open the
    file, read it to a a byte array, and writing it out to my output stream. However, I got corrupted data at my other end and it took me quite a bit of
    debugging hours to find out why.
    I tried writing to a file the same information I was writing to my output stream but, although my file was correct, my output stream still contained
    corrupted data.
    I think I have finally narrowed down my problem to the fact that when I try to write a 0x00 value to my output stream, everything gets ignored from this
    point until I write a 0x0a byte to my output stream. Obviously this produces corrupted data, since I need all the bytes of my binary file.
    If somebody can direct me as to how to write a 0x00 value to a socket output stream, I would really appreciate your input.
    This is a sample of my code (Right now I am writing byte by byte: not very efficient, but was the only way I found where I was getting a problem):
    // out is of type OutputStream
    if (Zipped){
         // In this case we just have to stream the file out
    File myFile = new File(fileName);
         FileInputStream inputFile = new FileInputStream(myFile);
         FileOutputStream fos = new FileOutputStream(new File("/tmp/zip.gz")); //zip.gz results in a good (not corrupted) file.
         ByteArrayOutputStream out1 = new ByteArrayOutputStream(1024);
         int bytes = (int)myFile.length();
         byte[] buffer = new byte[bytes];
         bytes = inputFile.read(buffer);
         while (bytes != -1) {
              out1.write(buffer,0,bytes);
              bytes = inputFile.read(buffer);
         inputFile.close();
         try {
              int count=0;
              bytes=out1.size();
              while (count<bytes){
                   fos.write(buffer,count,1);
    out.write(buffer,count,1); // when buffer[count] == 0x00, everything after it gets ignored until a 0x0a byte is written.
                   count++;
         } catch (IOException ioe) {
              SysLog.event("IOEXCEPTION: "+ioe);
         } catch (Exception e) {
              SysLog.event("EXCEPTION: "+e);
         fos.flush();
         fos.close();
         out.flush();
    out.close();
         return;
    }

    Actually, I had thought about that and for some time I tried getting rid of some of the header information in the gzipped file, and then I stopped doing that when I realized that part of the gzipped file is a CRC value which I believe is computed taking into account both the data of the file and the header information. If this is the case, then getting rid of part of the header would produce a corrupted file ... but then, I might be wrong in this issue.
    Anyway, to make my application more clear, what I am doing is writing the code for a cgi command which is suppossed to access information from a database, create an xml file, gzip it, and then send it to the client who requested the information.
    Therefore, what I am sending is an xml file (Content-Type: text/xml) in compressed format (Content-Encoding: gzip). Note that if I don't gzip the file and then send it uncompressed to the client, I have no problems. However, for me it is very important to gzip it because the size of the file can get very large and that would just take bandwidth unnecessarily.
    On the other hand, if I try to gunzip the file locally, I have no problem either (the file is not corrupt at the server's end). Therefore, my problem is when I stream it out.
    Any further help would be really appreciated!

  • Write output to binary file in C/C++?

    How can I write output to a file in binary format instead of ascii?
    Or... if I am using a buffer.. to write a buffer to binary format instead of ascii?
    I am writing a lot of data to a text file and I would like to reduce the size by writing it all into a binary file instead of ascii?
    Last edited by kdar (2012-12-13 14:36:41)

    Basic I/O?
    char buffer[100];
    // C++ style
    ofstream file;
    file.open ("data.bin", ios::out | ios::binary);
    file.write(buffer, sizeof buffer);
    /* C style */
    FILE* file2 = fopen("data2.bin", "wb");
    fwrite(buffer, sizeof buffer, 1, file2);
    A lot of people prefer the old C style functions for binary i/o even in C++ (and iostream for formatted i/o). Writing and readong more complex structures requires some castin, in C++ usually done with reinterpret_cast<char*>(someStructure).

  • Can TestStand open, read, write and close binary files?

    From a TestStand sequence, I need to open, read, write and close binary files. Does TestStand support this capability?

    Christine -
    In the past I have used the C/C++ Adapter to call directly into the CVI RTE functions. The CVI functions I have used are OpenFile, WriteFile and CloseFile. The functions are exported from the DLL as CVI_OpenFile, CVI_WriteFile, and CVI_CloseFile. I just had to make sure that on termination of the sequence, that the integer handle was properly released by calling the close function.
    If you are using LabVIEW, you could call directly into the low level VIs to do a similar set of operations.
    Scott Richardson (NI)
    Scott Richardson
    National Instruments

  • Write structure to binary file and append

    I have a very simple problem that I cannot seem to figure out, which I guess doesn't make it so simple, for me at least.  I've made a little example .vi which basically breaks down what I want to to.  I have a structure, in this instance 34 bytes of various data types.  I would like to iteratively write this structure to a binary file as the data in the ACTUAL structure in my .vi will be changing through time.  I'm having trouble getting the data to be appended to the binary file rather than overwriting it since the file size is always 34 bytes regardless of how many times I run the .vi or run the for loop.
    I'm not an expert, but it seems if I write a 34 byte structure to a file 10 times, the final product should be a binary file of 340 bytes (assuming I haven't padded or prepended with size).
    One really strange thing is that I keep getting error #1544 when I wire the refnum out wire to the file dialog input on the write function, but it works fine when I wire the file path directly to the write function.
    Can someone please swoop in and save me from this remedial task?
    Thanks for all the help. NI Forum rules!
    Solved!
    Go to Solution.
    Attachments:
    struct_to_bin.vi ‏13 KB

    Did you consider reading the text of the error message? Don't set the "disable buffering" input to true - just leave that input unwired. Why do you want to disable buffering?
    As a general practice, the file refnum should be stored in a shift register around the for loop instead of using loop tunnels, that way if you have zero iterations you will still get the file refnum passed through correctly. Also, there is no need for Set File Position inside the loop, since the file location is always the end of the last write unless it's specifically moved to another location. You might want it once outside the loop after you open the file, if you're appending to an existing file.

  • Coded UI - Test Results/Out - The binary files are getting copied to the TestResults/Out folder

    Hi,
    I am using Visual Studio 2013 for developing the Coded UI Automation Scripts.
    Whenever I execute a Coded UI Automation Script, the binary files are getting copied to the TestResults/Out folder.
    I do not want these binary files to be copied to OUT folder, and I have tried disabling the Deployment option under Test Settings > Deployment, but still the behavior is the same.
    Could you please advice, how to disable copying binary files into the Out directory.
    Thanks in Advance.
    Regards,
    Karthick K

    Hi Karthick K,
    From your description, as far as I know that when we enable Tracing and HtmlLogger for Coded UI Test and then run the coded UI test, we will get the Test Results/Out folder automatically.
    So it is default that the Out folder includes some files like codeduitestproject6.dll, CodedUITestProject6.pdb etc, we could not disable copying binary files into the Out directory.
    However, there have a replace workaround is I suggest you can disable Tracing and HtmlLogger for Coded UI Test. After you disable the Tracing and HtmlLogger for Coded UI Test, it will not generate the Out folder.
    Similarly, it will not get the binary files.
    http://blogs.msdn.com/b/visualstudioalm/archive/2012/11/08/enabling-htmllogger-in-coded-ui-test.aspx
    If you still want to this feature, I suggest you could submit this feature request:
    http://visualstudio.uservoice.com/forums/121579-visual-studio. The Visual Studio product team is listening to user voice there. You can send your idea there and people can vote. If
    you submit this suggestion, I hope you could post that link here, I will help you vote it.
    Thanks for your understanding.
    Best Regards,
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • What is the best combination of read and write objects to binary files ...

    Hi everyone,
    what are the best combination of read and write objects to read/write a record of customers from a binary file. I should be able to use StringTokenizer. I am bit confused with all these Stream reader and writers.
    abdul

    You are, indeed, confused:
    o StreamTokenizer works on character input, not binary input.
    o Readers and Writers in general work on character streams, not binary.
    That leaves you various input streams and output streams for reading binary data.

  • [solved] trying to figure out BASH configuration files

    I am trying to figure out where to properly place commands in the many stated/existing BASH configuration files —reading the wiki and the man page I see there are what at first glance are too many files:
    from: http://www.gnu.org/software/bash/manual … rtup-Files
    invoked as an interactive login shell or with --login:
    1. executes /etc/profile
    2. executes-first-one-of-the-following: ~/.bash_profile
    2. executes-first-one-of-the-following: ~/.bash_login
    2. executes-first-one-of-the-following: ~/.profile
    from: https://wiki.archlinux.org/index.php/bash#Configuration
    configuration file sourcing order at startup ... by default in arch:
    /etc/profile (indirectly) sources /etc/bash.bashrc
    /etc/skel/.bash_profile (which users are encouraged to copy to ~/.bash_profile) sources ~/.bashrc
    which means that /etc/bash.bashrc and ~/.bashrc will be executed for all interactive shells whether they are login shells or not
    It isn't clear to me what exactly means file#1 "indirectly sources" file#2: including ? append target file ? use file#2 instead of file#1 ?
    It isn't clear either if ~/.bash_login and/or ~/.bash_logout are ignored (or not) in arch
    so /etc/profile should be the (preferred) system-wide configuration file ?
    so ~/.bash_profile should be the (preferred) user-specific configuration file ?
    If so, does it means I should delete /etc/bash.bashrc (this is where I currently have system-wide commands) in favor of (ie: moving the content to) /etc/profile ?
    Last edited by ivanborodin (2014-06-10 16:28:21)

    First and foremost: thanks for the replies.
    Ziusudra wrote:As the Wiki states ...
    I am fully aware of what the wiki is stating —I even included the link in my first post. The problem is not that I am not aware of what it is stating, it is just I don't understand it and thus my post to begin with. More precisely; this highlighted line:
    Note: /etc/bash.bashrc is non-standard, only some distributions like Arch Linux have this included because packagers have added a -DSYS_BASHRC="/etc/bash.bashrc" flag at compilation time, in order to have a system bashrc file.
    which clearly states /etc/bach.bashrc is non-standard was the first hint to me that these files were legacy/going-to-be-deprecated/something-like-this (my assumption) in favor of newer (my assumption) /etc/profile and ~/.bash_profile
    Maybe I am asking some obvious question (this is why I am asking it in the newbie corner) but please, have in mind that for someone coming from the Windows world some things/concepts are totally alien at first sight, for eg: in Windows you start Powershell and you have one (only one) place to set your configuration options and the like. Here I am offered with:
    • 2 possible system-wide files: /etc/profile and /etc/bash.bashrc
    • 2 possible user-specific files: ~/.bash_profile and ~/.bashrc
    • 1 possible user-specific files: ~/.bash_login (and ~/.bash_logout)
    ... so I have (at least) 5 different places to execute commands upon login.
    I was carefully reading all the files present on my current installation and now I understand what "indirectly sourced" means: including (if the file is present just insert it here)
    I still don't understand what commands should be placed in which file; for eg: system-wide aliases should be placed in /etc/profile or in /etc/bash.bashrc ?
    One more time, I am not a rocket-scientist nor I am a dummy, but the wiki just states file precedence without offering any details as best practices or something like that and just figure out: that for such a basic-thing I am getting different answers from two users (someone with 6000+ posts) ... either one: or the wiki is not clear or this thing is a little more complex than it seems at first sight ... just my thought.
    Anyway, needless to say, thank you both for your time and patience.

  • Error trying to write to a measurement file and then send via gmail.vi

    So as the title says, I have a VI which is writing to a tdms file, and I want to send the file by gmail once the program has completed it's run. I downloaded gmail.vi and am integrating it into my existing code. Both my original code and the gmail VI work fine on their own, but when I try to have the combined VI send the data file by email, I get the error message that I've attached.
    Basically, it seems like the .tdms data file is in use by the measurement code, and I need to know how to free it up so that the gmail code can attach and send it.
    I've attached the main code as well as my modified gmail VI, so hopefully someone can take a look at tell me what I'm missing.
    Any help is much appreciate!
    Attachments:
    Error.PNG ‏19 KB
    PID Controller - Cleaned Up.vi ‏239 KB
    Email Alert.vi ‏19 KB

    While I can't view all of your code because it is missing several SubVIs, I have a good idea of what you are trying to do. I believe the culprit is the "Write to measurement file" express VI. Near as I can tell the original reference to the TDMS file is being held open inside the express VI. Here's a good general rule for using Express VI's: when you see them, RUN THE OTHER WAY! I say because there is almost always a significant amount of code in there that is unnecessary, In this case, you simply need the Open TDMS file, Write to TDMS, Close file functions; you do not need all of the extra code that is in the express VI's.
    <edit> P.S. I hope the username/password in Email alert.vi is a placeholder otherwise you just gave every idiot with access to LabVIEW your username and password to the account.
    Charles Chickering
    Architecture is art with rules.
    ...and the rules are more like guidelines

  • When trying to download it says binary file download and it starts but doesnt finish

    as i try to download the little box pops up and says binary file save or cancel . So i save and it starts downloading the little line starts across and it gets to the end and disappears and leaves a empty box and nothing else happens

    Which player? Do you mean Flash, or maybe the Windows Media Player plugin?
    * http://www.adobe.com/products/flashplayer/distribution3.html
    * https://support.mozilla.org/en-US/kb/play-windows-media-files-in-firefox
    You have to run the installers with Firefox closed.

  • How to read and write in a binary file?

    Hi all,
    how can i search in a binary file for string and replace this string with another
    bin file ...test.bin
    search String "DE AD FA 01"
    regards
    gebi

    You need to install Google on your computer. That way, you can search for all the information on that algorithm available on the Internet.

  • [SOLVED] Trying to write HLDS systemd service file

    Hey,
    I want to write a systemd service file for my HLDS AUR package: https://aur.archlinux.org/packages/hlds/
    My environment file looks like this:
    HLDS_USER=root
    HLDS_WORKINGDIR=/opt/hlds
    HLDS_PARAMS="-game cstrike"
    HLDS_LOGFILE=/var/log/hlds_cstrike
    The service file like this:
    After=network.target
    [Service]
    #EnvironmentFile=/etc/conf.d/hlds
    #User=${HLDS_USER}
    #WorkingDirectory=${HLDS_WORKINGDIR}
    ExecStart=/opt/hlds/hlds_run -game cstrike
    ExecStop=/bin/kill $MAINPID
    [Install]
    WantedBy=multi-user.target
    But systemd fails to start the service:
    hlds.service - Half-Life Dedicated Server
              Loaded: loaded (/usr/lib/systemd/system/hlds.service; disabled)
              Active: failed (Result: exit-code) since Thu, 2013-01-03 06:20:49 CET; 2s ago
             Process: 567 ExecStop=/bin/kill $MAINPID (code=exited, status=1/FAILURE)
             Process: 564 ExecStart=/opt/hlds/hlds_run -game cstrike (code=killed, signal=INT)
              CGroup: name=systemd:/system/hlds.service
    Jan 03 06:20:49 playground systemd[1]: Starting Half-Life Dedicated Server...
    Jan 03 06:20:49 playground systemd[1]: Started Half-Life Dedicated Server.
    Jan 03 06:20:49 playground hlds_run[564]: Invalid game type 'cstrike' sepecified.
    Jan 03 06:20:49 playground hlds_run[564]: Thu Jan  3 06:20:49 CET 2013: Server Failed
    Jan 03 06:20:49 playground kill[567]: usage: kill [ -s signal | -p ] [ -a ] pid ...
    Jan 03 06:20:49 playground kill[567]: kill -l [ signal ]
    Jan 03 06:20:49 playground systemd[1]: Unit hlds.service entered failed state
    Running
    /opt/hlds/hlds_run -game cstrike
    manually works quote well
    Someone knows how to proceed? Thanks
    Last edited by onny (2013-01-03 05:43:49)

    Solved. It didn't recognized the workingdir correctly ...

  • I m trying to write to a Mainframe file from informatica mapping. I have created the data map for tarrget file and have imported it in informatica designer.After completing the mapping when i try to run it gives me below error. Please help me out in resol

    This is not a PC/PWX problem, but a mainframe security problem. TIDSS01.POS.IBD.DR0100.DAT is not a GDG file name.It might be the base for a GDG. A GDG file name would look something like eitherTIDSS01.POS.IBD.DR0100.DAT(+1)orTIDSS01.POS.IBD.DR0100.DAT.G001V00 So I suspect that you have the wrong file name.Please talk with your mainframe team.

    HI Dan, I am very new to using power exchnage please help me through this issue, Below is the details : My data map name :postest.test1_POS
    Copy book name which is used to create the power exchange Datamap : TIDSS01.ACTRLP.TEST(POSCPY)
    Main frame GDG name : TIDSS01.POS.IBD.DR0100.DAT Below are the Session properties i have set in informatica workflow:Schema Name Override : postest
    Map Name Override : test1_POS
    PWX Partition Strategy : Overrides driven
    Space : CYLINDER
    File Name Override :TIDSS01.POS.IBD.DR0100.DAT(+1)
    I am getting below error: PWXPC_12190
    Message: [ERROR] Transformation [test1_POS]: A permanent error has been encountered in PowerExchange: [
    [Informatica][SCLI PWX Driver] PWX-00267 DBAPI Error. DB_INSERT failed for file postest.test1_POS.
    [Informatica][SCLI PWX Driver] PWX-01279 DBNTC INSERT failed for file postest.test1_POS. Rcs 1274/2019/268.
    [Informatica][SCLI PWX Driver] PWX-01274 DBNTC INSERT Failed for file postest.test1_POS, rcs 260/2019/268.
    [Informatica][SCLI PWX Driver] PWX-02019 SQL insert failure. SQLCODE = 268.
    [Informatica][SCLI PWX Driver] PWX-00268 DBAPI Error. DB_OPEN failed for file TIDSS01.POS.IBD.DR0100.DAT.
    [Informatica][SCLI PWX Driver] PWX-00220 DYNALLOC failed for file TIDSS01.POS.IBD.DR0100.DAT RCs = 9700/0.
    [Informatica][SCLI PWX Driver] PWX-00221 DATA SET: TIDSS01.POS.IBD.DR0100.DAT WITH RETURN CODE 08 REASON CODE 00   
    [Informatica][SCLI PWX Driver] PWX-00221 RACF FUNCTION: RACDEF FOR   
    [Informatica][SCLI PWX Driver] PWX-00221 IGD308I DATA SET ALLOCATION REQUEST FAILED -   
    [Informatica][SCLI PWX Driver] PWX-00221 IKJ56893I DATA SET TIDSS01.POS.IBD.DR0100.DAT NOT ALLOCATED+   
    [Informatica][SCLI PWX Driver] PWX-07404 Permanent error set by Open call on file "TIDSS01.POS.IBD.DR0100.DAT" because dynamic allocation failed. rc=4
    [Informatica][SCLI PWX Driver] PWX-07515 Insert call for table postest.test1_POS met a permanent error. Return codes 267 2019 268.
    ]

  • Can FileReader write to a binary file

    I am just wondering because i would like to use it for a program that is suppose to write in binary. If it can, should I just insert an array of characters that are 1 or 0 or should i actually figure out the bites involved.

    Readers read, not write.
    So you have a char[] that looks like this: {'0','1','0','0',...}? That's an odd thing to have. I would roll up my sleeves and convert it to a byte[]. Writing that to a file is a simple matter: use FileOutputStream.
    edit: can you avoid having a char[] in the first place?

  • Removing the user define comments while trying to write to the property fil

    Hi All,
    While using new Properies().setPropety(). It is removing the user define comments from property file.
    Could you please explain me why it is happening and how can I restrict it
    Thanks & Regards
    Edited by: Rakesh83 on Feb 20, 2008 4:33 AM

    Rakesh83 wrote:
    Could you please provde me sample code for this?
    #! /usr/bin/groovy
    xml = '''
    <config>
        <property name="foo" value="bar" comment="blah, blah, blah" />
        <property name="baz" value="qux" comment="doodle doodle dee" />
    </config>
    def config = new XmlParser().parse(new StringReader(xml))
    def property = config.property.find { it.'@name' =~ 'baz' }
    assert 'doodle doodle dee' == property.@comment
    property.@value = 'snorp'
    assert 'doodle doodle dee' == property.@comment // comments preserved!
    // write xml as needed~

Maybe you are looking for