输入流 FileInputStream(即将记事本的字节读取出来)
package wjq;
//字节流的输入 fileInputStream
import java.io.FileInputStream;
import java.io.IOException;
public class Example01{
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("c:\\java\\Example\\hh.txt");
int b = 0;
System.out.println("你文件的字节是");
while((b=in.read())!=-1) { //等于-1表示文件的字节读完了
System.out.println(b);
}
in.close();
}
}
运行结果
注:h对应ASCII编码中的104,其它的都是表中的对应关系
输出流 FileOutoutStream
package wjq;
//字节的输出 FileOutputStream
import java.io.FileOutputStream;
import java.io.IOException;
public class Example02{
public static void main(String[] args) throws IOException {
FileOutputStream out = new FileOutputStream("c:\\java\\Example\\rr.txt");
String str ="good";
out.write(str.getBytes());
out.close();
}
}
将good写入到文档中
3.文件的逐个拷贝
package wjq;
//文件的拷贝 逐个拷贝
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Example03{
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("c:\\java\\Example\\hh.txt");
FileOutputStream out = new FileOutputStream("c:\\java\\Example\\jj.txt");
int b = 0;
long beginTime = System.currentTimeMillis();
while((b=in.read())!=-1){
out.write(b);
}
long endTime = System.currentTimeMillis();
System.out.println("拷贝用的时间为"+(endTime-beginTime)+"毫秒");
in.close();
out.close();
}
}
4.使用字节缓冲区拷贝文件
package wjq;
//使用字节缓冲区拷贝文件
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Example04 {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("c:\\java\\Example\\hh.txt");
FileOutputStream out = new FileOutputStream("c:\\java\\Example\\n.txt");
int b= 0;
byte[] buff = new byte[1024];
long beginTime = System.currentTimeMillis();
while((b=in.read(buff))!=-1) {
out.write(buff,0,b);
}
long endTime = System.currentTimeMillis();
System.out.println("拷贝文件所用的时间为"+(endTime-beginTime)+"毫秒");
in.close();
out.close();
}
}
5.使用字节缓冲流拷贝文件
package wjq;
//使用字节缓冲流拷贝文件
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class EXample05 {
public static void main(String[] args) throws IOException {
BufferedInputStream bu = new BufferedInputStream
(new FileInputStream("c:\\java\\Example\\hh.txt"));
BufferedOutputStream bk = new BufferedOutputStream
(new FileOutputStream("c:\\java\\Example\\ggg.txt"));
int b = 0;
long beginTime = System.currentTimeMillis();
while((b=bu.read())!=-1) {
bk.write(b);
}
long endTime = System.currentTimeMillis();
System.out.println(endTime-beginTime);
bu.close();
bk.close();
}
}
package wjq1;
//字符流操作文件,从文件中逐个读取字符
import java.io.FileReader;
import java.io.IOException;
public class Example01{
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader("c:\\java\\Example\\hh.txt");
int b =0;
while((b=fileReader.read())!=-1) {
System.out.print((char)b);
}
fileReader.close();
}
}
package wjq1;
//字符流逐个写字符
import java.io.FileWriter;
import java.io.IOException;
public class Example02 {
public static void main(String[] args) throws IOException {
FileWriter h = new FileWriter("c:\\java\\Example\\ffff.txt");
h.write("一个人玩,\t\n");
h.write("没有人一起玩,\t\n");
h.write("好多人一起玩,\t\n");
h.write("都在玩,\t\n");
h.close();
}
}
package wjq1;
//使用字符缓冲区拷贝文件
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Example03 {
public static void main(String[] args) throws IOException {
FileReader gg = new FileReader("C:\\java\\Example\\ffff.txt");
FileWriter ww = new FileWriter("c:\\java\\Example\\vvvvvv.txt");
int b = 0;
char[] buff = new char[1024];
while((b=gg.read(buff))!=-1) {
ww.write(buff,0,b);
}
gg.close();
ww.close();
}
}
package wjq1;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//使用字符缓冲流拷贝文件
public class EXample04 {
public static void main(String[] args) throws IOException {
BufferedReader bb = new BufferedReader(new FileReader
("C:\\java\\Example\\ffff.txt"));
BufferedWriter hh = new BufferedWriter(new FileWriter
("c:\\java\\EXample\\khjh.txt"));
String str = null;
while((str = bb.readLine())!=null) {
hh.write(str);
hh.newLine();
}
bb.close();
hh.close();
}
}