重点说明:想要使用一个类的方法,必须了解它的方法有哪些,分别如何使用,有什么用途。因此必须学会查看Java API,这就相当于学汉字是需要查阅字典,如果单纯的敲代码,而不知其义,就无法理解为何使用该方法。
https://www.lanzous.com/i1vykcd(jdk1.8 API)
目录
4.从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合
IO练习1:
1.复制文本文件
1)字节流的四种方式
/*字节流复制文本文件一共4种方式*/
//方式一
/**基本字节流一次读写一个字节:
* @throws IOException */
private static void method1() throws IOException {
//1.创建基本字节输入流对象
FileInputStream fis=new FileInputStream("a.txt");
//2.创建基本字节输出流对象
FileOutputStream fos=new FileOutputStream("b.txt");
//3.开始复制
int by=0;
while((by=fis.read())!=-1){
fos.write(by);
}
//4.释放资源
fos.close();
fis.close();
}
//方式二
/**基本字节流一次读写一个字节数组
* @throws IOException */
private static void method2() throws IOException {
//1.创建基本字节输入流对象
FileInputStream fis=new FileInputStream("a.txt");
//2.创建基本字节输出流对象
FileOutputStream fos=new FileOutputStream("b.txt");
//3.开始复制
byte[] bys=new byte[1024];
int len=0;
while((len=fis.read(bys))!=-1){
fos.write(bys);
}
//4.释放资源
fos.close();
fis.close();
}
//方式三
/**高效字节流一次读写一个字节
* @throws IOException */
private static void method3() throws IOException {
//1.创建基本字节输入流对象
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("a.txt"));
//2.创建基本字节输出流对象
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("Cb.txt"));
//3.开始复制
int by=0;
while((by=bis.read())!=-1){
bos.write(by);
}
//4.释放资源
bos.close();
bis.close();
}
//方式四
/**高效字节流一次读写一个字节数组
* @throws IOException */
private static void method4() throws IOException {
//1.创建基本字节输入流对象
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("a.txt"));
//2.创建基本字节输出流对象
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("b.txt"));
//3.开始复制
byte[] bys=new byte[1024];
int len=0;
while((len=bis.read(bys))!=-1){
bos.write(bys);
}
//4.释放资源
bos.close();
bis.close();
}
2)字符流的5种方式
// 基本字符流,一次读取一个字符
private static void method1(String srcPath, String desPath)
throws IOException {
FileReader fr = new FileReader(srcPath);
FileWriter fw = new FileWriter(desPath);
int ch = 0;
while ((ch = fr.read()) != -1) {
fw.write(ch);
}
fr.close();
fw.close();
}
// 基本字符流,一次读取一个字符数组
private static void method2(String srcPath, String desPath)
throws IOException {
FileReader fr = new FileReader(srcPath);
FileWriter fw = new FileWriter(desPath);
char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1) {
fw.write(chs, 0, len);
}
fr.close();
fw.close();
}
// 高效流,一次读取一个字符
private static void method3(String srcPath, String desPath)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcPath));
BufferedWriter bw = new BufferedWriter(new FileWriter(desPath));
int ch = 0;
while ((ch = br.read()) != -1) {
bw.write(ch);
bw.flush();
}
br.close();
bw.close();
}
// 高效流,一次读取一个字符数组
private static void method4(String srcPath, String desPath)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcPath));
BufferedWriter bw = new BufferedWriter(new FileWriter(desPath));
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1) {
bw.write(chs, 0, len);
bw.flush();
}
br.close();
bw.close();
}
// 高效流,一次读取一行
private static void method5(String srcPath, String desPath) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcPath));
BufferedWriter bw = new BufferedWriter(new FileWriter(desPath));
String line=null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}
2.复制图片
/*类似于文本复制,使用字节流*/
3.把ArrayList集合中的字符串数据存储到文本文件
public class ArrayListToFileDemo {
public static void main(String[] args) throws IOException {
// 创建ArrayList结合对象
ArrayList<String> array = new ArrayList<String>();
// 添加集合元素
array.add("hello");
array.add("world");
array.add("java");
// 封装目的地
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
// 遍历集合,并把每一个元素写入文本文件
for (String s : array) {
// 写数据
bw.write(s);
// newLine() 写一行行分隔符。
bw.newLine();
// flush() 刷新流。
bw.flush();
}
//释放资源
bw.close();
}
}
4.从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合
public class FileToArrayListDemo {
public static void main(String[] args) throws IOException {
//封装数据源
BufferedReader br=new BufferedReader(new FileReader("a.txt"));
//创建集合对象
ArrayList<String> array = new ArrayList<String>();
//读取文本文件,每读取一行(每一行就是一个字符串数据),就添加到集合中
String line=null;
//readLine(),读取一行,如果已达到流的末尾,则为null
while((line=br.readLine())!=null){
array.add(line);
}
//释放资源
br.close();
/*//可以遍历集合,查看是否成功
for(String s:array){
System.out.println(s);
}*/
}
}
5.复制单极文件夹
/*
* 需求:复制单级文件夹:
* 数据源:e:\\demo
* 目的地:e:\\test
*
* 分析:
* A:封装目录
* B:获取该目录下所有文本的File数组
* C:遍历该File数组,得到每一个File对象
* D:把该File进行复制
* */
public class CopyFolderDemo {
public static void main(String[] args) throws IOException {
//封装目录
File srcFolder=new File("e:\\demo");
File destFolder=new File("e:\\test");
//如果目的文件夹不存在就创建
if(!destFolder.exists()){
destFolder.mkdir();
}
//获取该目录下所有文本的File数组
File[] fileArray = srcFolder.listFiles();
//遍历该File数组,得到每一个File对象
for(File file:fileArray){
String name = file.getName();
File newFile = new File(destFolder,name);
copyFile(file,newFile);
}
}
private static void copyFile(File file, File newFile) throws IOException {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(newFile));
byte[] bys=new byte[1024];
int len=0;
while((len=bis.read(bys))!=-1){
bos.write(bys, 0, len);
}
bis.close();
bos.close();
}
}
6.复制单极文件夹中指定文件并修改文件名称
public class CopyFolderDemo {
public static void main(String[] args) throws IOException {
// 封装数据源
File srcFolder = new File("e:\\java");
// 封装目的地
File destFolder = new File("e:\\jad");
// 获取该目录下java文件的File数组
File[] fileArray = srcFolder.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return new File(dir, name).isFile() && name.endsWith(".java");
}
});
// 遍历该File数组,获取每一个File对象
for (File file : fileArray) {
// 数据源:e:\java\DataTypeDemo.java
// 目的地:e:\\jad\DataTypeDemo.java
String name = file.getName();
File newFile=new File(destFolder,name);
copyFile(file, newFile);//这一步主要是把内容复制过来
}
//在目的地目录下改名
File[] destFileArray = destFolder.listFiles();
for(File destFile:destFileArray){
String name = destFile.getName();
String newName = name.replace(".java", ".jad");
File newFile=new File(destFolder,newName);
destFile.renameTo(newFile);
}
}
private static void copyFile(File file, File newFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
file));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(newFile));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}
7复制多极文件夹
/*
* 需求:复制多级文件夹
* 数据源:E:\JavaSE\day21\code\demos
* 目的地:E:\\
*
* 分析:
* 1.封装数据源File
* 2.封装目的地File
* 3.判断该文件夹是文件还是文件夹
* a:是文件夹
* 就在目的地目录下创建该文件夹
* 获取该File对象下所有文件或文件夹File对象
* 遍历得到每一个File对象
* 回到3
* b:是文件
* 就复制(字节流)
* */
public class CopyFoldersDemo {
public static void main(String[] args) throws IOException {
// 封装数据源File
File srcFile = new File("E:\\JavaSE\\day21\\code\\demos");
// 封装目的地File
File destFile = new File("E:\\");
// 复制文件夹的功能
copyFolder(srcFile, destFile);
}
private static void copyFolder(File srcFile, File destFile) throws IOException {
// 判断该File是文件还是文件夹
if (srcFile.isDirectory()) {
// 如果是文件夹,就在目的地目录下创建该文件夹
File newFolder = new File(destFile, srcFile.getName());
newFolder.mkdir();
// 然后获取该文件夹下的所有文件或文件夹File对象
File[] fileArray = srcFile.listFiles();
for (File file : fileArray) {
// 递归判断
copyFolder(file, newFolder);
}
} else {
// 如果是文件,就直接复制到该目录下
File newFile = new File(destFile, srcFile.getName());
copyFile(srcFile, newFile);
}
}
private static void copyFile(File srcFile, File newFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcFile));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(newFile));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}