标签:io流 字符流 bufferedoutputstream bufferedinputstream fileinputstream
3)FileOutputStream 用于写入诸如图像数据之类的原始字节的流。要写入字符流,请考虑使用 FileWriter 。
2、构造方法
1)不追加:
2)可追加
(二)功能
<span style="font-family:Arial;font-size:18px;"> //写数据
fos.write("hello,IO".getBytes());
fos.write("java".getBytes());</span><span style="font-family:Arial;font-size:18px;"> /*
* 字节输出流操作步骤:
* A:创建字节输出流对象
* B:调用write()方法
* C:释放资源
*
* public void write(int b):写一个字节
* public void write(byte[] b):写一个字节数组
* public void write(byte[] b,int off,int len):写一个字节数组的一部分
*/
public void test2() throws IOException{
// 创建字节输出流对象
// OutputStream os = new FileOutputStream("fos2.txt"); // 多态
FileOutputStream fos = new FileOutputStream("fos.txt");
// 调用write()方法
fos.write(97); //97 -- 底层二进制数据 -- 通过记事本打开 -- 找97对应的字符值 -- a
// fos.write(57);
// fos.write(55);
//public void write(byte[] b):写一个字节数组
byte[] bys={97,98,99,100,101};
fos.write(bys);
//public void write(byte[] b,int off,int len):写一个字节数组的一部分
fos.write(bys,1,3);
//释放资源
fos.close();
}</span> int by = 0;
// 读取,赋值,判断
while ((by = fis.read()) != -1) {
System.out.print((char) by);
}
// 释放资源
fis.close(); // 数组的长度一般是1024或者1024的整数倍
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
// System.out.println(len);
System.out.print(new String(bys, 0, len));
}
// 释放资源
fis.close();<span style="font-family:Arial;font-size:18px;"> /*
* 需求:把e:\\林青霞.jpg内容复制到当前项目目录下的mn.jpg中
*
* 数据源:
* e:\\林青霞.jpg --读取数据--FileInputStream
* 目的地:
* mn.jpg--写出数据--FileOutputStream
*/
public void test4() throws IOException{
// 封装数据源
FileInputStream fis = new FileInputStream("e:\\林青霞.jpg");
// 封装目的地
FileOutputStream fos = new FileOutputStream("mn.jpg");
// 复制数据
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
// 释放资源
fos.close();
fis.close();
//字节数据,图片、音乐等都可以处理
/* // 封装数据源
FileInputStream fis = new FileInputStream("e:\\哥有老婆.mp4");
// 封装目的地
FileOutputStream fos = new FileOutputStream("copy.mp4");
// 复制数据
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
// 释放资源
fos.close();
fis.close();*/
//利用字节数组处理
// 封装数据源
FileInputStream fiss = new FileInputStream("e:\\哥有老婆.mp4");
// 封装目的地
FileOutputStream foss = new FileOutputStream("copy.mp4");
// 复制数据
byte[] bys = new byte[1024];
int len = 0;
while ((len = fiss.read(bys)) != -1) {
foss.write(bys, 0, len);
}
// 释放资源
fos.close();
fis.close();
}</span> /*
* 计算机是如何识别什么时候该把两个字节转换为一个中文呢?
* 在计算机中中文的存储分两个字节:
* 第一个字节肯定是负数。
* 第二个字节常见的是负数,可能有正数。但是没影响。
*/
public void test5() {
// String s = "abcde";
// // [97, 98, 99, 100, 101]
String s = "我爱你中国";
// [-50, -46, -80, -82, -60, -29, -42, -48, -71, -6]
byte[] bys = s.getBytes();
System.out.println(Arrays.toString(bys));
} /*
* 需求:把e:\\哥有老婆.mp4复制到当前项目目录下的copy.mp4中
*
* 字节流四种方式复制文件:
* 基本字节流一次读写一个字节: 共耗时:117235毫秒
* 基本字节流一次读写一个字节数组: 共耗时:156毫秒
* 高效字节流一次读写一个字节: 共耗时:1141毫秒
* 高效字节流一次读写一个字节数组: 共耗时:47毫秒
*/
public void test3() throws IOException {
long start = System.currentTimeMillis();
// method1("e:\\哥有老婆.mp4", "copy1.mp4");
// method2("e:\\哥有老婆.mp4", "copy2.mp4");
// method3("e:\\哥有老婆.mp4", "copy3.mp4");
method4("e:\\哥有老婆.mp4", "copy4.mp4");
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "毫秒");
}
// 高效字节流一次读写一个字节数组:
public static void method4(String srcString, String destString)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
// 高效字节流一次读写一个字节:
public static void method3(String srcString, String destString)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcString));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destString));
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}
// 基本字节流一次读写一个字节数组
public static void method2(String srcString, String destString)
throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString);
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
// 基本字节流一次读写一个字节
public static void method1(String srcString, String destString)
throws IOException {
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(destString);
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}标签:io流 字符流 bufferedoutputstream bufferedinputstream fileinputstream
原文地址:http://blog.csdn.net/u012228718/article/details/46051643