1、在本机的磁盘系统中,找一个文件夹,利用File类的提供方法列出该文件夹中
的所有文件的文件名和文件的路径,执行效果如下: [必做题]
路径是xxx的文件夹内的文件有:
文件名: abc.txt
路径名: c:\temp\abc.txt
----------------------------------------
文件名: def.txt
路径名: c:\temp\def.txt
package pro_0423_homework;
import java.io.File;
/**
* 1、在本机的磁盘系统中,找一个文件夹,利用File类的提供方法列出该文件夹中
的所有文件的文件名和文件的路径,执行效果如下: [必做题]
路径是xxx的文件夹内的文件有:
文件名: abc.txt
路径名: c:\temp\abc.txt
----------------------------------------
文件名: def.txt
路径名: c:\temp\def.txt
*/
public class HomeWork01
{
public static void main(String[] args)
{
File f1=new File("E:\\Java");
String str=f1.getPath();
System.out.println("路径是"+str+"的文件夹内有:");
File[] files=f1.listFiles();
for(File file:files)
{
if(file.isFile())
{
System.out.println("文件名:"+file.getName());
System.out.println("路径名:"+file.getPath());
System.out.println("--------------------------");
}
}
}
}
2、编写一个java程序实现文件复制功能,要求将
d:/io/copysrc.doc中的内容复制到d:/io/copydes.doc中。[必做题]
package pro_0423_homework;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 2、编写一个java程序实现文件复制功能,要求将
d:/io/copysrc.doc中的内容复制到d:/io/copydes.doc中。[必做题]
*/
public class HomeWork02
{
public static void main(String[] args)
{
FileReader fr=null;
BufferedReader br=null;
FileWriter fw=null;
BufferedWriter bw=null;
try {
//1.
fr=new FileReader("d:/io/copysrc.txt");
br=new BufferedReader(fr);
fw=new FileWriter("d:/io/copydes.txt");
bw=new BufferedWriter(fw);
//2.
String str=br.readLine();
while(str!=null)//读取一行
{
bw.write(str);//写入一行
bw.newLine();//换行
str=br.readLine();
}
bw.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//3.
try {
if(br!=null)
{
br.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(fr!=null)
{
fr.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(bw!=null)
{
bw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(fw!=null)
{
fw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package pro_0423_homework;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
/**
* 2、编写一个java程序实现文件复制功能,要求将
d:/io/copysrc.doc中的内容复制到d:/io/copydes.doc中。[必做题]
*/
public class HomeWork0202 {
public static void main(String[] args) {
//doc不是纯文本文件,可能会有图片,要使用字节流而不是字符流
FileInputStream fis=null;
BufferedInputStream bis=null;
FileOutputStream fw=null;
BufferedOutputStream bos=null;
try {
fis=new FileInputStream("d:/io/copysrc.doc");
bis=new BufferedInputStream(fis);
fw=new FileOutputStream("d:/io/copydes.doc");
bos=new BufferedOutputStream(fw);
//2.读写操作
int lin=bis.read();//读
while(lin!=-1)
{
bos.write(lin);//写
lin=bis.read();
}
bos.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//3.
try {
if(bis!=null)
{
bis.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(fis!=null)
{
fis.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(bos!=null)
{
bos.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(fw!=null)
{
fw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package pro_0423_homework;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 2、编写一个java程序实现文件复制功能,要求将
d:/io/copysrc.doc中的内容复制到d:/io/copydes.doc中。[必做题]
*/
public class HomeWork020202
{
public static void main(String[] args)
{
//doc不是纯文本文件,可能会有图片,要使用字节流而不是字符流
try(FileInputStream fis=new FileInputStream("d:/io/copysrc.doc");
BufferedInputStream bis=new BufferedInputStream(fis);
FileOutputStream fw=new FileOutputStream("d:/io/copydes.doc");
BufferedOutputStream bos=new BufferedOutputStream(fw);)
{
//2.读写操作
int lin=bis.read();//读
while(lin!=-1)
{
bos.write(lin);//写
lin=bis.read();
}
bos.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3、创建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实现过滤器的功能
效果显示:
从文件c:/text.txt中读取的内容是: hello world
通过过滤流写到文件d:/test.txt中的内容是 : heLLo worLd
读写完毕
以下三种都是:
package pro_0423_homework;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 3、创建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实现过滤器的功能
效果显示:
从文件c:/text.txt中读取的内容是: hello world
通过过滤流写到文件d:/test.txt中的内容是 : heLLo worLd
读写完毕
*/
public class HomeWork03
{
public static void main(String[] args)
{
File f1=new File("e:/Java/text.txt");
if(!f1.exists())
{
try {
f1.createNewFile();//添加,创建文件
} catch (IOException e) {
e.printStackTrace();
}
}
try(FileWriter fw1=new FileWriter("e:/Java/text.txt");
BufferedWriter bw1=new BufferedWriter(fw1))
{
bw1.write("hello world");
} catch (IOException e1) {
e1.printStackTrace();
}
File f2=new File("d:/test.txt");
if(!f2.exists())
{
try {
f2.createNewFile();//添加,创建文件
} catch (IOException e) {
e.printStackTrace();
}
}
FileReader fr=null;
BufferedReader br=null;
FileWriter fw=null;
BufferedWriter bw=null;
try {
fr=new FileReader("e:/Java/text.txt");
br=new BufferedReader(fr);
fw=new FileWriter("d:/test.txt");
bw=new BufferedWriter(fw);
/*String str=br.readLine();
while(str!=null)
{
System.out.println("从文件c:/text.txt中读取的内容是:"+str);
str=br.readLine();
}*/
System.out.print("从文件c:/text.txt中读取的内容是:");
int ch=br.read();
while(ch!=-1)
{
System.out.print((char)ch);
if((char)ch=='l')
{
bw.write('L');
}else {
bw.write((char)ch);
}
ch=br.read();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(br!=null)
{
br.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(fr!=null)
{
fr.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(bw!=null)
{
bw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(fw!=null)
{
fw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println();
//读取写入的文件
FileReader fr2=null;
BufferedReader br2=null;
try {
fr2=new FileReader("d:/test.txt");
br2=new BufferedReader(fr2);
String str2=br2.readLine();
while(str2!=null)
{
System.out.println("通过过滤流写到文件d:/test.txt中的内容是:"+str2);
str2=br2.readLine();
}
System.out.println("读写完毕");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(br2!=null)
{
br2.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(fr2!=null)
{
fr2.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
方式三:
package pro_0423_homework;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 3、创建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实现过滤器的功能
效果显示:
从文件c:/text.txt中读取的内容是: hello world
通过过滤流写到文件d:/test.txt中的内容是 : heLLo worLd
读写完毕
*/
public class HomeWork0302
{
public static void main(String[] args)
{
File f1=new File("e:/Java/text.txt");
File f2=new File("d:/test.txt");
if(!f1.exists())
{
try {
f1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try(FileWriter fw1=new FileWriter(f1);)
{
fw1.write("hello world");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(!f2.exists())
{
try {
f2.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try(FileReader fr=new FileReader(f1);
BufferedReader br=new BufferedReader(fr);
FileWriter fw=new FileWriter(f2);
BufferedWriter bw=new BufferedWriter(fw);)
{
System.out.print("从文件c:/text.txt中读取的内容是:");
int ch=br.read();
while(ch!=-1)
{
System.out.print((char)ch);
if((char)ch=='l')
{
bw.write('L');
}else {
bw.write((char)ch);
}
ch=br.read();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//--
System.out.println();
try(FileReader fr2=new FileReader(f2);
BufferedReader br2=new BufferedReader(fr2);)
{
String str2=br2.readLine();
while(str2!=null)
{
System.out.println("通过过滤流写到文件d:/test.txt中的内容是:"+str2);
str2=br2.readLine();
}
System.out.println("读写完毕");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
第三种:
package pro_0423_homework;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 3、创建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实现过滤器的功能
效果显示:
从文件c:/text.txt中读取的内容是: hello world
通过过滤流写到文件d:/test.txt中的内容是 : heLLo worLd
读写完毕
*/
public class Ex0426_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f1 = new File("e:/Java/text.txt");
File f2 = new File("d:/test.txt");
if (!f1.exists()) {
try {
f1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
} // 添加
}
if (!f2.exists()) {
try {
f2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
} // 添加
}
try (FileWriter fw = new FileWriter(f1);
BufferedReader br = new BufferedReader(new FileReader(f1));
BufferedReader br1 = new BufferedReader(new FileReader(f2));
BufferedWriter bw = new BufferedWriter(new FileWriter(f2));) {
StringBuffer str = new StringBuffer("hello world");
fw.write(str.toString());// 写到C:\\test.txt
fw.flush();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'l') {
str.setCharAt(i, 'L');
}
}
bw.write(str.toString());// 写到D:\TEST1.TXT
bw.flush();
System.out.print("从" + f1.getPath() + "文件中读取的内容是:");
String s = br.readLine();
while (s != null) {
System.out.println(s.toString());
s = br.readLine();
}
System.out.print("通过过滤流写到文件" + f2.getPath() + "的内容是:");
String s2 = br1.readLine();
while (s2 != null) {
System.out.println(s2.toString());
s2 = br1.readLine();
}
System.out.println("读写完毕");
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
4、在程序中创建一个Student类型的对象,并把对象信息保存
到 d:/io/student.txt文件中,然后再从文件中把Student对象的信息
读出显示在控制台上, Student类的描述如下: [选做题]
Student
-id
-name
-birth
+toString()
以下三种方式:
package pro_0423_homework;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.io.Serializable;
import com.neusoft.io.Employee;
/**
* 4、在程序中创建一个Student类型的对象,并把对象信息保存
到 d:/io/student.txt文件中,然后再从文件中把Student对象的信息
读出显示在控制台上, Student类的描述如下: [选做题]
Student
-id
-name
-birth
+toString()
*/
public class HomeWork04
{
public static void main(String[] args)
{
Student s1=new Student(1,"张三","1月1日");
Student s2=new Student(2,"李四","2月2日");
FileOutputStream fos=null;
ObjectOutputStream oos=null;
try {
fos=new FileOutputStream("d:/io/student.txt");
oos=new ObjectOutputStream(fos);
oos.writeObject(s1);
oos.writeObject(s2);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(oos!=null)
{
oos.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(fos!=null)
{
fos.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileInputStream fin=null;
ObjectInputStream oin=null;
try {
//1.
fin=new FileInputStream("d:/io/student.txt");
oin=new ObjectInputStream(fin);
//2.
Student e1=(Student)oin.readObject();//反序列化方法 需要将Object强转为Employee
Student e2=(Student)oin.readObject();
System.out.println(e1.toString());
System.out.println(e2.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(oin!=null)
{
oin.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(fin!=null)
{
fin.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Student implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
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;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", birth=" + birth + "]";
}
}
方式二:
package pro_0423_homework;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Serializable;
public class HomeWork0402
{
public static void main(String[] args)
{
Student1 s1=new Student1(1,"张三","1月1日");
Student1 s2=new Student1(2,"李四","2月2日");
FileOutputStream fos=null;
//ObjectOutputStream oos=null;//这个是错的
BufferedOutputStream bout=null;
PrintWriter pw=null;
try {
//1.
fos=new FileOutputStream("d:/io/student1.txt");
//oos=new ObjectOutputStream(fos);//这个是错的
bout=new BufferedOutputStream(fos);
pw=new PrintWriter(bout);
//2
pw.println(s1);
pw.flush();
pw.println(s2);
pw.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
pw.close();
try {
if(bout!=null)
{
bout.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fos!=null)
{
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//-----------------------------------
FileReader fr=null;
BufferedReader br=null;
try {
//1.
fr=new FileReader("d:/io/student1.txt");
br=new BufferedReader(fr);
//2.
String str=br.readLine();
while(str!=null)//读取一行
{
System.out.println(str);
str=br.readLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//3.
try {
if(br!=null)
{
br.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(fr!=null)
{
fr.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Student1 implements Serializable
{
private static final long serialVersionUID = 253L;
private int id;
private String name;
private String birth;
public Student1() {
super();
}
public Student1(int id, String name, String birth) {
super();
this.id = id;
this.name = name;
this.birth = birth;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Student1 [id=" + id + ", name=" + name + ", birth=" + birth + "]";
}
}
方法三:
package pro_0423_homework;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 4、在程序中创建一个Student类型的对象,并把对象信息保存
到 d:/io/student.txt文件中,然后再从文件中把Student对象的信息
读出显示在控制台上, Student类的描述如下: [选做题]
Student
-id
-name
-birth
+toString()
*/
public class HomeWork040202
{
public static void main(String[] args)
{
Student s1=new Student(1,"张三","1月1日");
Student s2=new Student(2,"李四","2月2日");
try(FileOutputStream fos=new FileOutputStream("d:/io/student.txt");
BufferedOutputStream bout=new BufferedOutputStream(fos);
PrintWriter pw=new PrintWriter(bout);)
{
pw.println(s1);
pw.flush();
pw.println(s2);
pw.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try(FileReader fr=new FileReader("d:/io/student.txt");
BufferedReader br=new BufferedReader(fr);)
{
//2.
String str=br.readLine();
while(str!=null)
{
System.out.println(str);
str=br.readLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}