本文主要是介绍Java自学-IO流(4),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Java自学-IO流(4)
1、字符流复制Java文件改进版
package lesson04;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* Author: Gu Jiakai
* Date: 2021/8/27 19:15
* FileName: CopyJavaDemo02
* Description:
*/
public class CopyJavaDemo02 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("D:\\Java\\IO\\src\\lesson03\\StringDemo.java");
FileWriter fw = new FileWriter("Copy.java");
// int ch;
// while((ch=fr.read())!=-1){
// fw.write(ch);
// }
char[] chs=new char[1024];
int len;
while((len=fr.read(chs))!=-1){
fw.write(chs,0,len);
}
fw.close();
fr.close();
}
}
2、字符缓冲流
package lesson05;
import java.io.*;
/**
* Author: Gu Jiakai
* Date: 2021/8/27 19:28
* FileName: BufferedStreamDemo01
* Description:
*/
public class BufferedStreamDemo01 {
public static void main(String[] args) throws IOException {
// BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\Java\\IO\\bw.txt"));
// bw.write("hello\r\n");
// bw.write("world\r\n");
// bw.close();
BufferedReader br = new BufferedReader(new FileReader("D:\\Java\\IO\\bw.txt"));
//一次读取一个字符数据
// int ch;
// while((ch=br.read())!=-1){
// System.out.print((char) ch);
// }
//一次读取一个字节数组的数据
char[] chs=new char[1024];
int len;
while((len=br.read(chs))!=-1){
System.out.print(new String(chs,0,len));
}
br.close();
}
}
3、字符缓冲流复制Java文件
package lesson05;
import java.io.*;
/**
* Author: Gu Jiakai
* Date: 2021/8/27 19:40
* FileName: CopyJavaDemo01
* Description:
*/
public class CopyJavaDemo01 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("D:\\Java\\IO\\src\\lesson03\\StringDemo.java"));
BufferedWriter bw = new BufferedWriter(new FileWriter("copy.java"));
//一次读取一个字符数据
// int ch;
// while((ch=br.read())!=-1){
// bw.write(ch);
// }
//一次读取一个字符数组数据
char[] chs=new char[1024];
int len;
while((len=br.read(chs))!=-1){
bw.write(chs,0,len);
}
bw.close();
br.close();
}
}
4、字符缓冲流特有功能
package lesson05;
import java.io.*;
/**
* Author: Gu Jiakai
* Date: 2021/8/27 19:57
* FileName: BufferedStringDemo02
* Description:
*/
public class BufferedStringDemo02 {
public static void main(String[] args) throws IOException {
// BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));
//
// for (int i = 0; i < 10; i++) {
// bw.write("hello"+i);
// bw.newLine();
// bw.flush();
// }
//
// bw.close();
BufferedReader br = new BufferedReader(new FileReader("bw.txt"));
String line;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
}
}
5、字符缓冲流特有功能复制Java文件
package lesson05;
import java.io.*;
/**
* Author: Gu Jiakai
* Date: 2021/8/27 20:11
* FileName: CopyJavaDemo02
* Description:
*/
public class CopyJavaDemo02 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("D:\\Java\\IO\\src\\lesson03\\StringDemo.java"));
BufferedWriter bw = new BufferedWriter(new FileWriter("copy.java"));
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
}
6、IO流小结
这篇关于Java自学-IO流(4)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!