java--IO流(后半部分)

来源:互联网 发布:mac删掉windows 编辑:程序博客网 时间:2024/05/21 22:25

1.打印流

PrintWriter(Writerout)

打印输出流,针对于字符

@Test
public void test() throws IOException {
FileOutputStream fos = new FileOutputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/f.txt");
PrintStream ps = new PrintStream(fos);

ps.println("HelloMyJava");
ps.print("hahaha");

ps.close();
fos.close();
}

PrintStream(OutputStreamout)

打印输出流,针对于字节



@Test
public void test1() throws IOException {
FileWriter fw = new FileWriter("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/g.txt");
PrintWriter pw = new PrintWriter(fw);

pw.println("Hello我的Java世界");
pw.print("哈哈哈");
pw.print("嘿嘿嘿");

pw.close();
fw.close();
}


2.对象流

ObjectOutputStream(OutputStreamout)

对象输出流,配合FileOutputStream使用,将对象输出到磁盘

writeObject

将对象写入磁盘

@Test
public void test1() throws IOException {
User user1 = new User("张三", "3749827849237984", "123", 20000);
User user2 = new User("李四", "6867867858", "1111", 30);

FileOutputStream fos = new FileOutputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/zs.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos); //定义对象输出流
oos.writeObject(user1);
oos.close();
fos.close();


FileOutputStream fos1 = new FileOutputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/ls.dat");
ObjectOutputStream oos1 = new ObjectOutputStream(fos1);
oos1.writeObject(user2);
oos1.close();
fos1.close();
}

ObjectInputStream(InputStreamin)

对象输流,配合FileInputStream使,将对象输到程序

readObject

从磁盘中读取对象到程序

@Test
public void test2() throws IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/zs.dat");
ObjectInputStream ois = new ObjectInputStream(fis); //定义对象输入流
User zs = (User) ois.readObject(); //从磁盘路径中读取对象


System.out.println("姓名:"+zs.getName());
System.out.println("卡号:"+zs.getCardNo());
System.out.println("密码:"+zs.getPwd());
System.out.println("账户金额:"+zs.getMoney());

ois.close();
fis.close();
}


3.缓冲流

缓冲流对读写的数据提供了缓冲的功能,提高了读写的效率

readLine

法是BufferedReader对象提供的,⽅法返回值是String类型,表⽰每次读取⼀⾏的内容

newLine

法是BufferedWriter对象提供的,表⽰向⽂件中写⼊⼀个换⾏符


BufferedInputStream(FileInputStream in)

BufferedInputStream(FileInputStreamin, int sz)

/**
* 通过FileInputStream读取磁盘中的图片,借助缓冲区BufferedInputStream
* @throws IOException 
*/
@Test
public void test1() throws IOException {
FileInputStream fis = new FileInputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/a.txt");
BufferedInputStream bis = new BufferedInputStream(fis, 1024); //定义缓冲区

int b;
while((b=bis.read()) != -1) {
System.out.println((char)b);
}

bis.close();
fis.close();
}

BufferedOutputStream(FileOutputStream out)

BufferedOutputStream(FileOutputStreamout, int sz)

/**
* 通过FileOutputStream输出内容到磁盘文件,借助缓冲区BufferedOutputStream
* @throws IOException 
*/
@Test
public void test2() throws IOException {
FileOutputStream fos = new FileOutputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/b.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos, 1024); //定义缓冲区

bos.write("HelloMyJava".getBytes());

bos.close();
fos.close();
}

BufferedReader(Reader in)

BufferedReader(Readerin, int sz)

/**
* 通过FileReader读取文件内容,借助缓冲区BufferedReader
* @throws IOException 
*/
@Test
public void test3() throws IOException {
FileReader fr = new FileReader("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/hello.txt");
BufferedReader br = new BufferedReader(fr, 1024); //定义缓冲区

int c;
while((c=br.read()) != -1) {
System.out.println((char)c);
}

br.close();
fr.close();
}

BufferedWriter(Writer out)

BufferedWriter(Writerout, int sz)

/**
* 通过FileWriter向磁盘文件写入内容,借助缓冲区BufferedWriter
* @throws IOException 
*/
@Test
public void test4() throws IOException {
FileWriter fw = new FileWriter("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/hello/c.txt");
BufferedWriter bw = new BufferedWriter(fw, 1024);

bw.write("嘿嘿嘿");

bw.close();
fw.close();
}


案例:

@Test
public void testcopyAB() throws IOException{
File f1=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/A.txt");
File f2=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/B.txt");
f1.createNewFile();
f2.createNewFile();
FileOutputStream fos=new FileOutputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/A.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos,1024);
bos.write("fsadfdsffGREG".getBytes());
bos.close();
fos.close();

FileInputStream fis=new FileInputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/A.txt");
BufferedInputStream bis=new BufferedInputStream(fis,1024);
FileOutputStream fosB=new FileOutputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/B.txt");
BufferedOutputStream bosB=new BufferedOutputStream(fosB,1024);

int b=0;
while((b=bis.read())!=-1){
bosB.write(b);
}
System.out.println("拷贝完成 ");
bosB.close();
fosB.close();
bis.close();
fis.close();

}

@Test
public void testfilerwcopyABC() throws IOException{
File f11=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/A.txt");
File f22=new File("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/B.txt");
f11.createNewFile();
f22.createNewFile(); 
FileWriter fw=new FileWriter("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/A.txt");
BufferedWriter bw=new BufferedWriter(fw,1024);
bw.write("我爱大自然~~~");
bw.newLine();
bw.write("sure");
bw.close();
fw.close();

FileReader fr=new FileReader("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/A.txt");
BufferedReader br=new BufferedReader(fr,1024);
FileWriter FW=new FileWriter("C:/Users/Administrator/Workspaces/MyEclipse 8.5/2017.12.14/B.txt");
BufferedWriter BW=new BufferedWriter(FW,1024);

String str;
while((str=br.readLine())!=null){//读取文件,按行读取
BW.write(str);
BW.newLine();
}
System.out.println("拷贝完成 ");
BW.close();
FW.close();
br.close();
fr.close();
}

原创粉丝点击