FileOutputStream和FileInPutputStream流的联合使用

来源:互联网 发布:啊哈c语言 小木虫 编辑:程序博客网 时间:2024/05/17 21:07
package com.luo.io;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


/**
 * 利用FileInputStream和FileOutputStream从磁盘中的某个
 * 文件夹中读取内容,然后写入到磁盘中其它的文件夹中
 *
 */


public class FileOutPutStreamTest01 {

public static void main(String[] args) {

FileInputStream fis = null;
FileOutputStream fos = null;
try {
String filePath = "d:\\xiaozhu.txt";
fis = new FileInputStream(filePath);
byte[] input = new byte[1024];
int temp = 0;
String context = null;
while ((temp = fis.read(input)) != -1) {
context = new String(input, 0, temp);
}
fos = new FileOutputStream("D:\\xiaoluo.txt");
String mes = context;
byte[] bytes = mes.getBytes();
fos.write(bytes);
fos.flush();   //从内存中读取的数据强制写入到磁盘中的某个文件中
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭文件
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
0 0
原创粉丝点击