public class MyTest2 {
public static void main(String[] args) throws IOException {
//采用字符流,来复制,文本文件
InputStreamReader in = new InputStreamReader(new FileInputStream("MyTest.java"));
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("MyTest2.java"));
//读一个字符,写一个字符来复制
int len=0;//用来记录你读取到的那个字符
while ((len=in.read())!=-1){
out.write(len);
out.flush();//字符流记得刷新一下
}
in.close();
out.close(); //关闭并刷新
}
}
字符流的5种写数据的方式
方法概述 public void write(int c) 写一个字符 public void write(char[] cbuf) 写一个字符数组 public void write(char[] cbuf,int off,int len) 写一个字符数组的 一部分 public void write(String str) 写一个字符串 public void write(String str,int off,int len) 写一个字符串的一部分
方法概述 public int read() 一次读取一个字符 public int read(char[] cbuf) 一次读取一个字符数组 如果没有读到 返回-1
字符流复制文本文件(用数组获取数据)
public class MyTest3 {
public static void main(String[] args) throws IOException {
//一次读取一个字符数组,写出一个字符数组 推荐 使用
InputStreamReader in = new InputStreamReader(new FileInputStream("MyTest.java"));
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("MyTest2.java"));
char[] chars = new char[1000];
int len=0;//读取到有效字符个数
while ((len=in.read(chars))!=-1){
out.write(chars,0,len);
out.flush();//字符流记得刷新一下
}
in.close();
out.close();
}
}
高效的字符流 高效的字符输出流: BufferedWriter 构造方法: public BufferedWriter(Writer w) 高效的字符输入流: BufferedReader 构造方法: public BufferedReader(Reader e)
案例演示:
public class MyTest {
public static void main(String[] args) throws IOException {
//高效的字符流
//BufferedReader
// BufferedWriter
// BufferedReader从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
// 可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。
BufferedReader bfr = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bfw = new BufferedWriter(new FileWriter("aa.txt"));
char[] chars = new char[1024];
int len = 0;
while ((len=bfr.read(chars)) != -1) {
bfw.write(chars, 0, len);
bfw.flush();
}
bfr.close();
bfw.close();
}
}
字符缓冲流的特殊功能
字符缓冲流的特殊功能 BufferedWriter: public void newLine():根据系统来决定换行符 具有系统兼容性的换行符 BufferedReader: public String readLine():一次读取一行数据 是以换行符为标记的 读到换行符就换行 没读到数据返回null,包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
案例演示: 字符缓冲流的特殊功能
public class MyTest1 {
public static void main(String[] args) throws IOException {
// BufferedReader从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
// 可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。
BufferedReader bfr = new BufferedReader(new FileReader("回顾.java"));
BufferedWriter bfw = new BufferedWriter(new FileWriter("aaa.java"));
//BufferedReader 他有一个特有的方法,readLine() 一次读取一行内容
//bfr.readLine()
// bfw.write("\r\n");
//bfw.newLine(); //写一个换行符
//读取一行,写出一行来复制
String line=null;
while ((line=bfr.readLine())!=null){
bfw.write(line);
bfw.newLine(); //写出一个换行符,具有平台兼容性
bfw.flush();
}
bfr.close();
bfw.close();
}
}
把集合中的数据存储到文本文件
案例演示: 需求:把ArrayList集合中的字符串数据存储到文本文件 分析:
a: 创建一个ArrayList集合
b: 添加元素
c: 创建一个高效的字符输出流对象
d: 遍历集合,获取每一个元素,把这个元素通过高效的输出流写到文本文件中
e: 释放资源
public class MyTest {
public static void main(String[] args) throws IOException {
ArrayList<String> list = new ArrayList<>();
list.add("张飞");
list.add("赵云");
list.add("马超");
list.add("黄忠");
list.add("关羽");
FileWriter writer = new FileWriter("username.txt");
for (String s : list) {
writer.write(s);
writer.write("\r\n");
writer.flush();
}
writer.close();
}
}
把文本文件中的数据存储到集合中
案例演示: 需求:从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合 分析:
a: 创建高效的字符输入流对象
b: 创建一个集合对象
c: 读取数据(一次读取一行)
d: 把读取到的数据添加到集合中
e: 遍历集合
f: 释放资源
public class MyTest2 {
public static void main(String[] args) throws IOException {
ArrayList<String> list = new ArrayList<>();
BufferedReader bfr = new BufferedReader(new FileReader("username.txt"));
String line = null;
while ((line = bfr.readLine()) != null) {
list.add(line);
}
for (String s : list) {
System.out.println(s);
}
}
}
public class MyTest3 {
public static void main(String[] args) throws IOException {
ArrayList<String> list = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("username.txt")));
//读取一行数据,往集合里面添加一个
String line=null;
while ((line=reader.readLine())!=null){
list.add(line);
}
//随机从集合里面抽取一个人
Random random = new Random();
int index = random.nextInt(list.size());
String s = list.get(index);
System.out.println(s);
}
}
复制多级文件夹
案例演示: 需求: 复制D:\course这文件夹到E:\course 分析:
a: 封装D:\测试图片 为一个File对象
b: 封装E:\测试图片 为一个File对象,然后判断是否存在,如果不存在就是创建一个目录
c: 获取a中的File对应的路径下所有的文件对应的File数组
d: 遍历数组,获取每一个元素,进行复制
e: 释放资源
public class Demo {
public static void main(String[] args) throws IOException {
File file = new File("D:\\测试图片");
File file1 = new File(file.getAbsolutePath().replace("D", "E"));
if(!file1.exists()){
file1.mkdirs();
}
copyFolder(file,file1);
}
private static void copyFolder(File file, File file1) throws IOException {
File[] files = file.listFiles();
for (File f : files) {
if(f.isFile()){
copyFile(f,file1);
}else {
file=new File(f.getAbsolutePath());
file1=new File(file.getAbsolutePath().replace("D","E"));
if(!file1.exists()){
file1.mkdirs();
}
copyFolder(file,file1);
}
}
}
private static void copyFile(File f, File file1) throws IOException {
f = new File(f.getAbsolutePath());
file1 = new File(f.getAbsolutePath().replace("D","E"));
FileInputStream in = new FileInputStream(f);
FileOutputStream out = new FileOutputStream(file1);
byte[] bytes = new byte[1024 * 8];
int len=0;
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
}
in.close();
out.close();
}
}