import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class IOpractic01 {
public static void main(String[] args) {
//文件路径
File file = new File("D:\\example.txt");
//字节流写入文件
outPut(file);
//字节流读取文件
input(file);
//字符流写入文件
writeOut(file);
//字符流读取文件
readerIn(file);
//文件coppy
copyFile("D:\\example.txt","D:\\example1.txt");
bufferOutPut(file);
writeOutPut(file);
}
/**
* 字节输出流
* @param file
*/
static void outPut(File file){
String str="亲爱的 我的头有点晕乎";
FileOutputStream out =null;
try {
out= new FileOutputStream(file);
byte[] byt = str.getBytes();
out.write(byt);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 字节输出流
* @param file
*/
static void input(File file){
FileInputStream in =null;
try {
in = new FileInputStream(file);
byte[] bte = new byte[1024];
int len =in.read(bte);
System.out.println("文件中的信息是:"+ new String(bte,0,len));
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 字符输入流
* @param file
*/
static void readerIn(File file){
FileReader read =null;
try {
read = new FileReader(file);
char[] car = new char[1024];
int len = read.read(car);
System.out.println("输出的文件结果为:"+new String(car,0,len));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 字符输出流
*/
static void writeOut(File file){
String str ="十年之前,你不认识我,我不认识你";
FileWriter write =null;
try {
write = new FileWriter(file);
write.write(str);
write.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 文件复制
* @param dest
* @param path
*/
static void copyFile(String path,String dest){
File file = new File(dest);
FileInputStream in = null;
FileOutputStream out = null;
try {
//如果文件不存在,则创建
if(!file.exists()){
file.createNewFile();
}
in = new FileInputStream(path);
out = new FileOutputStream(file);
byte[] byt = new byte[1024];
int len;
while((len=in.read(byt))!=-1){
for(int i =0;i<len;i++){
out.write(byt[i]);
}
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/*____________________________下面是加入缓冲区_______________________________*/
static void bufferOutPut(File file){
String str="我是一个小例子";
FileOutputStream out =null;
try {
//这里第二个参数true,是为了不覆盖原有文件,在原有文件后继续添加
out = new FileOutputStream(file,true);
BufferedOutputStream buffer = new BufferedOutputStream(out);
byte[] byt =str.getBytes();
buffer.write(byt);
//清空缓冲区
buffer.flush();
//缓冲区关闭
buffer.close();
//输出流关闭
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//字符流
static void writeOutPut(File file){
String str="我只是一个简单的例子1111";
FileWriter out = null;
try {
out = new FileWriter(file,true);
BufferedWriter buffer = new BufferedWriter(out);
buffer.write(str);
buffer.flush();
buffer.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
java IO 流 读写文件 以及复制
最新推荐文章于 2023-09-23 07:00:00 发布