习题1
(1)在判断e盘下是否有文件夹mytemp ,如果没有就创建mytemp(2)在e:\lmytemp目录下,创建文件hello.txt
(3)如果hello.txt已经存在,提示该文件已经存在,就不要再重复创(4)并且在hello.txt文件中,写入hello,world~
代码1
package file;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Homework01 {
public static void main(String[] args) {
String dirPath = "E:\\mytemp";
File dirfile = new File(dirPath);
if (dirfile.exists()){
String filePath = "E:\\mytemp\\hello.txt";
File file = new File(filePath);
if (file.exists()){
System.out.println("该文件已存在,请勿重复创建!");
}else {
try {
file.createNewFile();
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));
bufferedWriter.write("hello,world");
System.out.println("成功写入!");
bufferedWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}else {
dirfile.mkdir();
System.out.println("文件创建成功!");
}
}
}
运行结果1
习题2
要求:使用BufferedReader读取一个文本文件,为每行加上行号,再连同内容一并输出到屏幕上.
代码
package file;
import java.io.*;
public class Homework02 {
public static void main(String[] args) throws IOException {
String filePath = "E:\\mytemp\\test.txt";
BufferedReader br = new BufferedReader(new FileReader(filePath));//读
try {
String line;
int lineNum = 0;
while ((line = br.readLine()) != null){
System.out.println(++lineNum + " " + line);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
br.close();
}
}
}
运行结果
习题3
题目描述
(1)要编写一个dog.properties
name=tom
age=5
color=red
(2)编写Dog类(name,age,color)创建一个dog对象,读取dog.成属性初始化,并输出
(3)将创建的Dog对象,序列化到文件dog.dat文件
代码2
package file;
import java.io.*;
import java.util.Properties;
public class Homework03 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
/* properties.setProperty("name","tom");
properties.setProperty("age","5");
properties.setProperty("corlor","red");
properties.store(new FileWriter("\\src\\file\\dog.propperties"),"小狗tom的配置文件");*/
String filePath = "src\\file\\dog.propperties";
properties.load(new FileReader(filePath));
properties.list(new PrintStream(filePath));
String name = properties.getProperty("name") + "";
int age = Integer.parseInt(properties.get("age") + "");
String corlor = properties.getProperty("corlor") + "";
Dog tom = new Dog(name, age, corlor);
//System.out.println(tom); //标准输出
BufferedReader br = new BufferedReader(new FileReader(filePath)); //字符流输出
String line;
System.out.println("dog对象的信息:");
while ((line = br.readLine()) != null){
System.out.println(line);
}
br.close();
//序列化dog
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src\\file\\dog.dat"));
oos.writeObject(tom);
}
}
class Dog implements Serializable {
private String name;
private int age;
private String corlor;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCorlor() {
return corlor;
}
public void setCorlor(String corlor) {
this.corlor = corlor;
}
public Dog(String name, int age, String corlor) {
this.name = name;
this.age = age;
this.corlor = corlor;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
", corlor='" + corlor + '\'' +
'}';
}
}
运行结果3