Java文件与流-练习

1、在本机的磁盘系统中,找一个文件夹,利用File类的提供方法,列出该文件夹中的所有文件的文件名和文件的路径,执行效果如下:
路径是xxx的文件夹内的文件有:
文件名:abc.txt
路径名:d:\temp\abc.txt
--------------------------------------------
文件名:def.txt
路径名:d:\temp\def.txt

public class test {
	public static void main(String[] args) {
		File file1 = new File("d:/temp/abc.txt");
		if(file1.exists()) {
			System.out.println("文件已存在");
		}else {
			try {
				System.out.println("创建成功:"+file1.createNewFile());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		File file2 = new File("d:/temp/def.txt");
		if(file2.exists()) {
			System.out.println("文件已存在");
		}else {
			try {
				System.out.println("创建成功:"+file2.createNewFile());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		File file = new File("d:/temp");
		printFile(file);
	}
	public static void printFile(File file) {
		System.out.println("路径是d:/temp的文件夹内的文件有:");
		File[] fileList = file.listFiles();
		for (File f : fileList) {
			System.out.println("文件名:"+f.getName());
			System.out.println("路径名:"+f.getAbsolutePath());
		}
	}
}

改进版:输出该目录下的所有内容,包括子目录中的内容

public class test2 {
	public static void main(String[] args) {
		File file1 = new File("d:/temp/abc.txt");
		if(file1.exists()) {
			System.out.println("文件已存在");
		}else {
			try {
				System.out.println("创建成功:"+file1.createNewFile());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		File file2 = new File("d:/temp/def.txt");
		if(file2.exists()) {
			System.out.println("文件已存在");
		}else {
			try {
				System.out.println("创建成功:"+file2.createNewFile());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		File file = new File("d:/temp");
		printFile(file);
	}
	public static void printFile(File file) {
		System.out.println("路径是d:/temp的文件夹内的文件有:");
		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);
			}
		}
	}
}

结果:

在这里插入图片描述

2、创建c:/test.txt文件并在其中输入"hello world"
创建一个输入流读取该文件中的文本
并且把小写的l变成大写L再利用输出流写入到d:\test.txt中
3.1 实现步骤:
3.1.1 在本地硬盘C盘下创建一个文件test.txt
3.1.2 创建一个包含main()方法的类,并在main中编写代码
3.1.3 运行代码并且测试结果
3.2 实现过滤器的功能

public static void main(String[] args) {

		FileReader reader = null;
		FileWriter writer = null;
		try {
			reader = new FileReader("c:/test.txt");
			writer = new FileWriter("d:/test.txt");
			char[] c = new char[3];
			int length;
			while((length=reader.read(c))!=-1) {
			
				System.out.println(length+Arrays.toString(c));
				c = new String(c).replace("l","L").toCharArray();				
				writer.write(c,0,length);
		
			}
			writer.flush();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				reader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}

结果:
在这里插入图片描述

在这里插入图片描述

3、在程序中创建一个Student类型的对象,并把对象信息保存到
d:/io/student.txt文件中,然后再从文件中把Student
对象的信息读出显示在控制台上,Student类的描述如下:

在这里插入图片描述

//创建Student类实现可用于序列化操作的Serializable接口

public class Student implements Serializable{
	private int id;
	private String name;
	private String birth;
	
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public Student(int id, String name, String birth) {
		super();
		this.id = id;
		this.name = name;
		this.birth = birth;
	}
	
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", birth=" + birth + "]";
	}
	
}

public class test {

	public static void main(String[] args) {
		
		Serialization();
		deSerialization();
		
	}
	public static void Serialization() {
		ObjectOutputStream output = null;
		
		try {
			Student stu = new Student(1,"小明","2000年1月1号");
			
			FileOutputStream out = new FileOutputStream("d:/io/student.txt");
			
			output = new ObjectOutputStream(out);
			
			output.writeObject(stu);
			
			output.flush();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
		
			try {
				if(output!=null) {
					output.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
	public static void deSerialization() {
		ObjectInputStream input = null;
		
		try {
			
			FileInputStream in = new FileInputStream("d:/io/student.txt");
			
			input =new ObjectInputStream(in);
			
			Student stu = (Student) input.readObject();
			
			System.out.println(stu.toString());
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
		
			try {
				if(input!=null) {
					input.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}		
	}		
}

结果:
在这里插入图片描述

4、编写一个java程序实现文件复制功能,要求将d:/io/copysrc.doc中的内容复制到d:/io/copydes.doc中。

public class BufferedTest {
	public static void main(String[] args) {
		BufferedReader reader = null;
		BufferedWriter writer = null;
		
		try {
			FileReader r = new FileReader("d:/io/copysrc.doc");
			reader = new BufferedReader(r);
			
			FileWriter w = new FileWriter("d:/io/copydes.doc");
			writer = new BufferedWriter(w);
			
			String length = "";
			while((length=reader.readLine())!=null) {
				System.out.println(length);
				writer.write(length);
				writer.newLine();
				
			}
			writer.flush();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(reader!=null) {
					reader.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if(writer!=null) {
					writer.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}

有任何问题,欢迎随时与我讨论交流!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值