29.二进制文件的读写

import java.io.*;

//二进制文件的读写
public class ReadAndWriteBinaryFile {
    public static void main(String[] args){
        DataInputStream dis=null;
        DataOutputStream dos=null;
        FileInputStream fis=null;
        FileOutputStream fos=null;

        try {
            //创建输入流对象
            fis=new FileInputStream("c:\\myDoc\\star.jpg");
            dis=new DataInputStream(fis);
            //创建输出流对象
            fos=new FileOutputStream("c:\\myDoc\\new.jpg");
            dos=new DataOutputStream(fos);
            //读取文件并写入文件
            int temp;
            while((temp=dis.read())!=-1){
                dos.write(temp);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{

            try {
                if(dis!=null){
                    dis.close();
                }
                if(dos!=null){
                    dos.close();
                }
                if(fis!=null){
                    fis.close();
                }
                if(fos!=null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}
时间: 2024-08-11 09:53:16

29.二进制文件的读写的相关文章

简单Java程序向实用程序的过度:二进制文件的读写

File I/O中常见的文件读写: 1.字节流读写文本文件 FileInputStream; FileOutputStream; 2.字符流读写文本文件 FileReader; FileWriter; BufferedReader; BufferedWriter; 3.二进制读写文件 DataInputStream; DataOutputStream; 这里重点介绍二进制文件的读写: 一.使用字节流类DataInputStream读写二进制文件 DataInputStream类是FileInpu

C++学习49 对二进制文件的读写操作

二进制文件不是以ASCII代码存放数据的,它将内存中数据存储形式不加转换地传送到磁盘文件,因此它又称为内存数据的映像文件.因为文件中的信息不是字符数据,而是字节中的二进制形式的信息,因此它又称为字节文件. 对二进制文件的操作也需要先打开文件,用完后要关闭文件.在打开时要用ios::binary指定为以二进制形式传送和存储.二进制文件除了可以作为输入文件或输出文件外,还可以是既能输入又能输出的文件.这是和ASCII文件不同的地方. 用成员函数read和write读写二进制文件 对二进制文件的读写主

将日期和时间作为 struct tm型的值直接向二进制文件进行读写

#include <stdio.h> #include <time.h> char data_file[]="D:\\%\\datetime.dat"; void get_data(void) { FILE *fp; time_t t; if((fp = fopen(data_file,"r")) == NULL) printf("本程序第一次运行!\n"); else { fread(&t,sizeof(time

原 BinaryWriter和BinaryReader(二进制文件的读写)

原文 BinaryWriter和BinaryReader(二进制文件的读写) C#的FileStream类提供了最原始的字节级上的文件读写功能,但我们习惯于对字符串操作,于是StreamWriter和 StreamReader类增强了FileStream,它让我们在字符串级别上操作文件,但有的时候我们还是需要在字节级上操作文件,却又不是一个字节 一个字节的操作,通常是2个.4个或8个字节这样操作,这便有了BinaryWriter和BinaryReader类,它们可以将一个字符或数字按指定 个数字

文本文件和二进制文件的读写问题

因为工作的需要,需要读写自定义文件格式,要求以二进制方式进行读写,数据对用户来说是不可见的. 首先考虑用C++实现,代码如下: 1 std::ofstream fout("test.in", std::ios::binary|std::ios::out); 2 std::string str("Hello, world!"); 3 double nNum = 100.00; 4 fout.write((char*)nNum,sizeof(double)); 5 fo

二进制文件的读写

一.使用字节流类DataInputStream读二进制文件 (1) 引入相关类 Import java.io.*;(DataInputStream, FileInputStream) (2) 构造一个数据输入流对象 FileInputStream fis=new FileInputStream ("C:\\ HelloWord. class"); DataInputStream dis=new DataInputStream (fis); (3) 利用数据输入流类的方法读二进制文件 d

c++二进制文件的读写

#include "stdafx.h" #include "string" #include <fstream> using namespace std; class C { public: C():i(),str(){};//初始化,非赋值 C(int iP,string strP):i(iP),str(strP){}; private: int i; string str; }; void main() { char *filePath = &quo

open语句对文本和二进制文件的读写

文本文件的操作此种方式是以行为单位进行读取的基本单位,主要应用的方法和函数有Open,Close,Line Input,FreeFile,EOF等.先简述其功能然后结合代码示例进行说明.Open:顾名思义,它的作用是打开文件,换而言之打开某个文件就是获得某个的控制权,一般情况下当文件处于打开状态时只有打开者才能对它进行操作.打开文件时要指定一个整数作为文件号,以后的操作都是针对这个代号进行的,而不是针对文件名.文件号也叫句柄,在程序中一个文件号只能指向一个文件,不能出现两个文件同时具有相同句柄的

【转】二进制文件的读写浮点数

原文网址:http://www.myexception.cn/c/243743.html ------解决方案--------------------#include <sys\stat.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int i; float farr[5]={1.0, 2.0, 3.0, 4.0, 5.0}, *farray; FILE *fp