1.读取文件
package com.yfs.javase; import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.Reader; public class IODemo1 { /**
* 读取文件 显示
*/
public static void main(String[] args) throws Exception {
//readFileSteame();
readFileChar();
} public static void readFileChar() throws Exception {
//字节流
Reader byte= new FileReader("FileDemo1.java"); int data = byte.read();
while(data != -1) {
System.out.print((char)data);
data = byte.read();
}
read.close();
} public static void readFileSteame() throws Exception {
//File file = new File("FileDemo1.java");//源头
//输入流 源头 目的 连接 字节流
//InputStream in = new FileInputStream(file) ;
InputStream in = new FileInputStream("FileDemo1.java");
//程序 目的
int data = in.read();//读取文件数据
while(data != -1){//读到-1 文件结束
System.out.print((char)data);
data = in.read();
} in.close();
} }
2.输出流
package com.yfs.javase; import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStream;
import java.io.Writer; public class IODemo2 { /**
* 输出流
*/
public static void main(String[] args) throws Exception {
//outDataSteame();
outDataChar();
} public static void outDataChar() throws Exception {
Writer write = new FileWriter("outchar.txt");
write.write('a');//处理一个字节
write.write(5);
write.write('中');//数据丢失 write.close();
} public static void outDataSteame() throws Exception {
File file = new File("out.txt");
//输出流
OutputStream out = new FileOutputStream(file);
out.write('a');//处理一个字节
out.write(5);
out.write('中');//数据丢失 out.close(); } }
3.复制文件
package com.yfs.javase; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer; public class IODemo3 { /**
* 复制文件
*/
public static void main(String[] args) throws Exception {
// copyFileStreame();
copyFileChar();
} public static void copyFileChar() throws Exception {
Reader in = new FileReader("E:/img/index.gif"); Writer out = new FileWriter("C:/pic1.gif"); int data = in.read();
while (data != -1) {
out.write(data);
data = in.read();
} in.close();
out.close();
System.out.println("文件复制完成..."); } public static void copyFileStreame() throws Exception {
InputStream in = new FileInputStream("E:/img/index.gif"); OutputStream out = new FileOutputStream("C:/pic.gif"); int data = in.read();
while (data != -1) {
out.write(data);
data = in.read();
} in.close();
out.close();
System.out.println("文件复制完成...");
} }
4.缓冲流
package com.yfs.javase; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer; public class IODemo4 { /**
* 复制文件 缓冲流
*/
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
// copyFileStreame();
//copyFile();
//copyFile1();
copyFile2();
long end = System.currentTimeMillis();
System.out.println("复制文件使用 " + (end - start) + " 毫秒"); }
public static void copyFile2() throws Exception {
File source = new File("FileDemo1.java");
InputStream in = new FileInputStream(source); File target = new File("C:/", source.getName());
OutputStream out = new FileOutputStream(target);
//自定义缓冲区
byte[] data = new byte[1024 * 4];//1k
int len = in.read(data);//len 读取的数据长度
while(len != -1 ) {//8
out.write(data, 0, len);
len = in.read(data);
}
in.close();
out.close(); } public static void copyFile1() throws Exception {
File source = new File("E:/EditPlus.zip");
InputStream in = new FileInputStream(source); File target = new File("C:/", source.getName());
OutputStream out = new FileOutputStream(target);
//自定义缓冲区
byte[] data = new byte[1024 * 4];//1k
int len = in.read(data);//len 读取的数据长度
while(len != -1 ) {//8
out.write(data, 0, len);
len = in.read(data);
}
in.close();
out.close(); } public static void copyFile() throws Exception {
// 缓冲流
File source = new File("E:/EditPlus.zip");
InputStream in = new FileInputStream(source); File target = new File("C:/", source.getName());
OutputStream out = new FileOutputStream(target);
// 缓冲流套接在节点流
BufferedInputStream bufin = new BufferedInputStream(in); BufferedOutputStream bufout = new BufferedOutputStream(out); int data = bufin.read();
while (data != -1) {
bufout.write(data);
data = bufin.read();
}
in.close();
out.close();
} public static void copyFileStreame() throws Exception {
File source = new File("E:/EditPlus.zip");
InputStream in = new FileInputStream(source); File target = new File("C:/", source.getName());
OutputStream out = new FileOutputStream(target); int data = in.read();
while (data != -1) {
out.write(data);
data = in.read();
} in.close();
out.close();
} }
5.流转换
package com.yfs.javase; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer; public class IODemo5 { /**
* 流转换 字节流 -》 字符流
*/
public static void main(String[] args) throws Exception { InputStream in = new FileInputStream("FileDemo1.java");
showFile(in); } public static void showFile(InputStream in) throws Exception {
Reader read = new InputStreamReader(in);//流转换 int data = read.read(); while(data != -1) {
System.out.print((char)data);
data = read.read();
}
in.close(); } }