之前我们博客所讲的IO流到这里就告一段落,我们来做一些练习结束IO流。
练习一:
- 在判断d盘下是否有文件夹mytmep,如果没有就创建mytemp。
- 在d:\111\mytemp 目录下,创建文件hello.txt
- 如果hello.txt 已经存在,提示该文件已经存在,就不要再重复创建了。
- 并且在hello.txt 文件中,写入hello,world~。
public class HomeWork01 {
public static void main(String[] args) throws IOException {
String directoryPath = "d:\\111\\mytemp";
File file = new File(directoryPath);
if(!file.exists()){
//创建
if(file.mkdirs()){
System.out.println("创建 "+ directoryPath+ "创建成功");
}else{
System.out.println("创建 "+ directoryPath+ "创建失败");
}
}
String filePath = directoryPath +"\\hello.txt";
file = new File(filePath);
if(!file.exists()){
//创建文件
if (file.createNewFile()){
System.out.println(filePath+"创建成功~");
//如果文件存在,我们就是用BufferedWriter 字符输入流写入文件。
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
bufferedWriter.write("hello,world~~~");
bufferedWriter.close();
}else{
System.out.println(filePath+"创建失败~");
}
}else{
System.out.println(filePath + " 已经存在,不在重复创建。。。");
}
}
}
练习二:
要求:使用BufferedReader读取一个文本文件,为每行加上行号,再连同内容一并输出到屏幕上。如果修改了文件的编码,出现中文乱码。
public class HomeWork02 {
public static void main(String[] args) {
String filePath = "d:\\111\\story.txt";
BufferedReader br = null;
String line = "";
int lineNum = 0;
try {
br =new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"gbk"));
while ((line = br.readLine()) != null){
System.out.println(++lineNum +line);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (br != null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
练习三:
- 要编写一个dog.properties
name=tom
age=5
color=red
- 编写Dog 类(name,age,color) 创建一个dog对象,读取dog.properties 用相应的内容完成属性初始化,并输出。
- 将创建的Dog 对象,序列化到文件dog.dat文件。
public class HomeWork03 {
public static void main(String[] args) throws IOException {
String filePath = "src\\com\\dog.properties";
Properties properties = new Properties();
properties.load(new FileReader(filePath));
String name = properties.get("name") + ""; //Object ->String
int age = Integer.parseInt(properties.get("age")+"");//Object -> int
String color = properties.get("color")+""; //Object -> String
Dog dog = new Dog(name, age, color);
System.out.println("====dog对象信息====");
System.out.println(dog);
//将创建的Dog对象 ,序列化到文件 dog.dat文件
String serFilePath = "d:\\111\\dog.dat";
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(serFilePath));
oos.writeObject(dog);
oos.close();
System.out.println("dog对象实例化完成!");
}
@Test
public void m1() throws IOException, ClassNotFoundException {
String serFilePath = "d:\\111\\dog.dat";
ObjectInputStream osi = new ObjectInputStream(new FileInputStream(serFilePath));
Dog dog = (Dog)osi.readObject();
System.out.println("====反序列化后 dog");
System.out.println(dog);
osi.close();
}
}
class Dog implements Serializable{
private String name;
private int age;
private String color;
public Dog(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
}