FileInputStream和FileOutputStream、DataInputStream和DataOutputStream、FileReader和Fil

阅读更多

package cn.zzu.lxzqz;

 

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.FileWriter;

 

public class ReadAndWrite {

 

/**

* FileInputStream和FileOutputStream

* 可以读取和写各类文件,一般用于读取获取写入诸如图像数据之类的原始字节流

* @throws Exception

*/

public static void readAndWrite() throws Exception{

File file = new File("D:\\lxzqz\\DSC_1042.JPG");//图片

//File file = new File("D:\\lxzqz\\JSP中的include的两种用法.doc");//word

//File file = new File("D:\\lxzqz\\YaHei.Consolas.1.12.ttf");//不常用文件

//File file = new File("D:\\lxzqz\\mv\\唯一--音悦Tai.mp4");//视频

FileInputStream read = new FileInputStream(file);

byte[] by = new byte[100];

FileOutputStream write = new FileOutputStream(new File("D:\\lxzqz\\mv\\图片.jpg"));

//FileOutputStream write = new FileOutputStream(new File("D:\\lxzqz\\mv\\jsp.doc"));

//FileOutputStream write = new FileOutputStream(new File("D:\\lxzqz\\mv\\简体.ttf"));

//FileOutputStream write = new FileOutputStream(new File("D:\\lxzqz\\mv\\zhang.mp4"));

while(read.read(by) > 0) {

write.write(by);

}

read.close();

write.close();

}

 

/**

* DataInputStream和DataOutputStream

* 可以读取和写各类文件,跟上面的方法差不多,允许从底层输入、输出流中读取基本 Java 数据类型(servlet)

* @throws Exception

*/

public static void readAndWrite(int i) throws Exception{

File file = new File("D:\\lxzqz\\系统问题.doc");//word

//File file = new File("D:\\lxzqz\\YaHei.Consolas.1.12.ttf");//不常用文件

//File file = new File("D:\\lxzqz\\mv\\唯一--音悦Tai.mp4");//视频

DataInputStream read = new DataInputStream(new FileInputStream(file));

byte[] by = new byte[100];

DataOutputStream write = new DataOutputStream(new FileOutputStream(new File("D:\\lxzqz\\mv\\jsp3.doc")));

//DataOutputStream write = new DataOutputStream(new FileOutputStream(new File("D:\\lxzqz\\mv\\jianti.ttf")));

//DataOutputStream write = new DataOutputStream(new FileOutputStream(new File("D:\\lxzqz\\mv\\唯一.mp4")));

while(read.read(by) > 0) {

write.write(by);

}

read.close();

write.close();

}

 

/**

* 用于读取、写入字符流文件

* 用来读取字符文件的便捷类

* @throws Exception

*/

public static void fileReaderAndWriter() throws Exception{

File file = new File("D:\\lxzqz\\表命名.txt");//word

FileReader read = new FileReader(file);

char[] ch = new char[100];

FileWriter write = new FileWriter("D:\\lxzqz\\mv\\table.txt");

while(read.read(ch) > 0) {

write.write(ch);

}

read.close();

write.close();

}

 

public static void main(String[] args) throws Exception{

//readAndWrite();

//readAndWrite(0);

//fileReaderAndWriter();

}

}

 

你可能感兴趣的:(java,各种文件读写流的使用方式和区别)