java 文件 填空题_Java练习题 - 文件

在本机的磁盘系统中,找一个文件夹,利用File类的提供方法,列出该文件夹中的所有文件的文件名和文件的路径,执行效果如下:

路径名:c:\temp\def.txt

public class Test {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

File f = new File("c:\\temp");

String[] s = f.list();

for(String o :s){

System.out.println("文件名"+o);

System.out.println("绝对路径"+f.getAbsolutePath()+"\\"+o);

>System.out.println("=================================");

}

}

}

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

public class FileCopy {

public static void main(String args[]){

//实例化File对象

File f1 = new File("d:/io/copysrc.doc");

File f2 = new File("d:/io/copydes.doc");

FileInputStream fr = null;

FileOutputStream fw = null;

try {

//实例化文件输入流和输出流对象

fr = new FileInputStream(f1);

fw = new FileOutputStream(f2);

byte[] cbuf = new byte[fr.available()];

//通过输入流,读取原文件中的数据到字节数组cbuf中

fr.read(cbuf);

//将字节数组中的数据,写到文件输出流中

fw.write(cbuf);

System.out.println("文件复制成功");

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

finally{

try {

//关闭输入输出流

fr.close();

fw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

创建c:/test.txt文件并在其中输入"hello world"

创建一个输入流读取该文件中的文本

并且把小写的l变成大写L再利用输出流写入到d:\test.txt中

实现步骤:1.在本地硬盘C盘下创建一个文件test.txt

2.创建一个包含main()方法的类,并在main中编写代码

3.运行代码并且测试结果

实现过滤器的功能

效果显示:

4bf259e9dc67

public class Test {

public static void main(String[] args) {

FileReader f;

String s="";

BufferedReader br=null;

try {

f = new FileReader("c:\\test.txt");

br = new BufferedReader(f);

s = br.readLine();

System.out.println("源文件"+s);

s=s.replace("l", "L");

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {

br.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

BufferedWriter bw =null;

System.out.println(s);

try {

FileWriter fw = new FileWriter("d:\\test.txt");

bw = new BufferedWriter(fw);

bw.write(s);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {

bw.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

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

class Student implements Serializable{

private static final long serialVersionUID = 1L;

String id;

String name;

String birth;

public Student(String id,String name,String birth){

this.id = id;

this.name = name;

this.birth = birth;

}

public String toString(){

return "id="+id+" name="+name+" birth="+birth;

}

}

public class SerializationExercise {

public static void main(String args[]){

Student s1 = new Student("A001","Lucy","1986-06-06");

try {

FileOutputStream fos = new FileOutputStream("d:\\io\\student.txt");

ObjectOutputStream oos = new ObjectOutputStream(fos);

System.out.println("对象序列化...");

oos.writeObject(s1);

oos.flush();

oos.close();

} catch (FileNotFoundException e1) {

System.out.println(e1.getMessage());

} catch (IOException e2) {

System.out.println(e2.getMessage());

}

try {

FileInputStream fis = new FileInputStream("d:\\io\\student.txt");

ObjectInputStream ois = new ObjectInputStream(fis);

System.out.println("反序列化...");

Student ss1 = (Student)ois.readObject();

System.out.println(ss1);

ois.close();

} catch (FileNotFoundException e) {

System.out.println(e.getMessage());

} catch (IOException e) {

System.out.println(e.getMessage());

} catch (ClassNotFoundException e) {

System.out.println(e.getMessage());

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值