练习1:
(1)判断e盘下是否有mytemp文件夹,如果没有就创建该文件夹
(2)在该目录下创建文件hello.txt 并写入内容 hello world!
(3)如果该文件已存在,提示文件已存在。
package com.io.test;
import java.io.*;
/**
* @author 华子
* 本章作业:判断e盘下是否有文件夹mytemp,如果没有就创建文件夹
* 并在该目录下创建hello.txt文件,并写入内容 hello,world!
* 如果已经存在,则提示该文件已存在
*
*/
public class Demo01 {
public static void main(String[] args) throws IOException {
String directoryPath = "e:\\mytemp";
File file = new File(directoryPath);
if (!file.exists()){
if(file.mkdirs()){
System.out.println("目录创建成功");
}else {
System.out.println("目录创建失败");
}
}else {
System.out.println("目录已经存在");
}
String filePath = directoryPath + "\\hua.txt";
File file2 = new File(filePath);
if (!file2.exists()){
if(file2.createNewFile()){
System.out.println("文件创建成功");
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file2));
bufferedWriter.write("hello,world!");
bufferedWriter.close();
}else{
System.out.println("文件创建失败");
}
}else {
System.out.println("文件已经存在");
}
}
}
练习2:
使用BufferedReader 读取一个文本文件,每行加上一个分号一并输出到屏幕上,并使用转换流制定编码方式
package com.io.test;
import java.io.*;
/**
* 本章作业2:使用BufferedReader 读取一个文本文件,每行加上一个分号一并输出到屏幕上
*/
public class Demo02 {
public static void main(String[] args) throws IOException {
String filePath = "e:\\story.txt";
String line = "";
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(filePath), "gbk");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
while((line = bufferedReader.readLine()) != null){
System.out.println(line+";");
}
bufferedReader.close();
}
}
练习3:
(1)编写一个dog.properties,
name = tom
age = 5
color = red
(2)编写Dog 类,获取一个dog对象,读取dog.properties并对Dog类的属性完成相应的初始化
(3)将Dog类的属性序列化到dog.dat中。
package com.io.test;
import java.io.*;
import java.util.Properties;
/**
* @author 华子
* 本章作业03:编写一个dog.properties,
* name = tom
* age = 5
* color = red
* 读取dog.properties并完成相应的初始化
*/
public class Demo03 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// 定义路径
String filePath = "F:\\IdeaProjects\\JavaSE\\Java基础语法\\src\\dog.properties";
// 创建dog.properties配置文件
Properties properties = new Properties();
properties.setProperty("name","tom");
properties.setProperty("age","5");
properties.setProperty("color","red");
properties.store(new FileOutputStream(filePath),null);
System.out.println("创建dog.properties成功");
System.out.println("------------------------------");
// 开始初始化Dog类的属性
// 读取配置文件
properties.load(new FileInputStream(filePath));
String name = properties.getProperty("name");
int age = Integer.parseInt(properties.getProperty("age"));
String color = properties.getProperty("color");
Dog dog = new Dog(name, age, color);
System.out.println(dog);
System.out.println("------------------------------");
String filePath02 = "e:\\dog.dat";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath02));
oos.writeObject(dog);
oos.close();
System.out.println("Dog类属性序列化成功");
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath02));
Object object = ois.readObject();
System.out.println(object);
}
}
运行结果: