2023年9月22日第十一章(I\O流与文件)课后题
1.拷贝一张 照片 从C 盘根目录到 D 盘根目录 (使用 节点流 实现 )
package ktp_0922_11;
import java.io.*;
/**
* [第十一章第一题]
*
* @author 秦帅2128424055
* @date 2023-9-22
* 拷贝一张 照片 从C 盘根目录到 D 盘根目录 (使用 节点流 实现 )
*
*/
public class Zy_01 {
public static void main(String[] args) throws IOException {
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis=new FileInputStream("C:\\0921\\0923_11_01\\02.jpg");
fos=new FileOutputStream("D:\\Ktp_0923_11\\02.jpg");
byte b[]=new byte[1024];//每次循环读取1024字节
int a=0;
while (true) {
a=fis.read(b);//读取文件
if (a==-1) {
break;
}
fos.write(b,0,a);//写入文件(数组中的数据,开始下角标,读取到的字节长度)
}
fos.flush();
} catch (Exception e) {
}finally{
try {
if (fis!=null) {
fis.close();//关闭输入流
}
if (fos!=null) {
fos.close();//关闭输出流
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.拷贝一张 照片 从C 盘根目录到 D 盘根目录 (使用 处理流 实现 )
package ktp_0922_11;
import java.io.*;
/**
* [第十一章第二题]
*
* @author 秦帅2128424055
* @date 2023-9-22
* 拷贝一张 照片 从C 盘根目录到 D 盘根目录 (使用 处理流 实现 )
*
*/
public class Zy_02 {
public static void main(String[] args) throws IOException {
copyFile();
}
//文本文件复制(字节流)
public static void copyFile() throws IOException {
File file=new File("C:\\0921\\0923_11_01\\01.jpg");
FileInputStream in=null;
BufferedInputStream bis=null;
File file2=new File("D:\\Ktp_0923_11\\01.jpg");
FileOutputStream out=null;
BufferedOutputStream bos=null;
try {
in=new FileInputStream(file);
bis=new BufferedInputStream(in);
out = new FileOutputStream(file2);
bos=new BufferedOutputStream(out);
//读取到的字节的长度
int b;
//bis.read() 每次读取到的字节数,注意此处给b重新赋值 ,否则死循环
while((b=bis.read())!=-1){
//把b长度的字节写入到bos里
bos.write(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bos.close();
out.close();
bis.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.创建文件 Demo1.txt 写入文本 hello 创建文件 Demo2.txt 写入文本 Neuedu 将两个文件内容 提取出来输出到 第三个文件 Test.txt 通过文件与流方式实现
package ktp_0922_11;
import java.io.*;
/**
* [第十一章第三题]
*
* @author 秦帅2128424055
* @date 2023-9-22
* 创建文件 Demo1.txt 写入文本 hello
* 创建文件 Demo2.txt 写入文本 Neuedu
* 将两个文件内容 提取出来输出到 第三个文件 Test.txt
* 通过文件与流方式实现
*/
public class Zy_03 {
public static void main(String[] args) throws IOException {
test01();
String sf1 = "D:\\Ktp_0923_11\\Demo1.txt"; //源文件
String sf2 = "D:\\Ktp_0923_11\\Demo2.txt"; //源文件
String df = "D:\\Ktp_0923_11\\Test.txt"; //目标文件
copy01(sf1, df);
copy01(sf2, df);
}
// 拷贝文件内容
public static void copy01(String sf, String df) {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(sf);
// fw=new FileWriter(df); 原内容被覆盖
fw = new FileWriter(df, true); //true表示在文件末尾附加新内容
int len = 0;
while ((len = fr.read()) != -1) {
fw.write((char) len);
}
System.out.println("复制完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 创建两个文件并写入内容
public static void test01() throws IOException {
File dir = new File("D:\\Ktp_0923_11\\Test.txt");
File f1 = new File("D:\\Ktp_0923_11\\Demo1.txt");
File f2 = new File("D:\\Ktp_0923_11\\Demo2.txt");
if (!f1.exists()) { //如果文件不存在
if (!dir.exists()) { //如果目录不存在
dir.mkdir(); //创建目录
}
f1.createNewFile(); //在磁盘上创建文件
}
if (!f2.exists()) { //如果文件不存在
if (!dir.exists()) { //如果目录不存在
dir.mkdir(); //创建目录
}
f2.createNewFile(); //在磁盘上创建文件
}
FileWriter fw1 = null;
FileWriter fw2 = null;
try {
fw1 = new FileWriter(f1);
String[] a1 = {"hello"};
for (String s : a1) {
fw1.write(s);
fw1.write("\r\n");
}
fw1.flush();
fw2 = new FileWriter(f2);
String[] a2 = {"Neuedu"};
for (String s : a2) {
fw2.write(s);
fw2.write("\r\n");
}
fw2.flush();
System.out.println("ok");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fw1 != null)
fw1.close();
if (fw2 != null)
fw2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.在本机的磁盘系统中,找一个文件夹,利用File类的提供方法,列出该文件夹中的所有文件的文件名和文件的路径,执行效果如下:
路径是xxx的文件夹内的文件有:
文件名:abc.txt
路径名:c:\temp\abc.txt
--------------------------------------------
文件名:def.txt
路径名:c:\temp\def.txt
import java.io.File;
import java.io.IOException;
/**
* [第十一章第四题]
*
* @author 秦帅2128424055
* @date 2023-9-22
* 在本机的磁盘系统中,找一个文件夹,利用File类的提供方法,列出该文件夹中的所有文件的文件名和文件的路径,执行效果如下:
* 路径是xxx的文件夹内的文件有:
* 文件名:abc.txt
* 路径名:c:\temp\abc.txt
* --------------------------------------------
* 文件名:def.txt
* 路径名:c:\temp\def.txt
*/
public class Zy_04 {
public static void main(String[] args) {
File file1 = new File("C:\\0921\\0923_11_01\\abc.txt");
if(file1.exists()) {
System.out.println("文件已存在");
}else {
try {
System.out.println("创建成功:"+file1.createNewFile());
} catch (IOException e) {
e.printStackTrace();
}
}
File file2 = new File("C:\\0921\\0923_11_01\\def.txt");
if(file2.exists()) {
System.out.println("文件已存在");
}else {
try {
System.out.println("创建成功:"+file2.createNewFile());
} catch (IOException e) {
e.printStackTrace();
}
}
File file = new File("C:\\0921\\0923_11_01");
printFile(file);
}
public static void printFile(File file) {
System.out.println("路径是C:\\0921\\0923_11_01的文件夹内的文件有:");
File[] fileList = file.listFiles();
for (File f : fileList) {
System.out.println("文件名:"+f.getName());
System.out.println("路径名:"+f.getAbsolutePath());
System.out.println("是否文件夹:"+f.isDirectory());//判断是否为文件夹
//如果为文件夹,输出文件夹中的内容
if(f.isDirectory()) {
printFile(f);
}
}
}
}