第一题:
1、使用Scanner类解析文件。
(1)准备文件test.txt内容,内含字符、数字等文本信息;
(2)使用Scanner类读取里面的字符信息和数字信息分别输出。
package etc.sdut.edu.cn;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) throws FileNotFoundException {
//建立一个新的Scanner ,它的生成值从下面这个指定文件中扫描
Scanner sc = new Scanner(new File(".\\src\\test.txt")) ;
String str = "" ; //用来接收文本文档
while(sc.hasNext()) {
String str1 = sc.nextLine();//用一个String来接受文本文件
//上一步需要注意一点是工作区的编码必须要和文本文档的编码一致。
str +=str1 ;
}
String s1[] = str.split("\\d+") ; //用来接受文本中的字符
String s2[] = str.split("\\D+") ; //用来接受文本中的数字
//输出数字
for(String s:s2)
{
System.out.print(s);
}
System.out.println();
//输出字符
for(String s:s1)
{
System.out.print(s);
}
System.out.println();
sc.close(); //关闭扫描器
}
}
第二题
2、利用对象输入/输出流进行文件的读写。
根据要求编写程序:
(1)创建类Student,内含学号、姓名、年龄、班级等属性,重写父类的toString()方法,用于输出学生的信息;
(2)建立测试类,建立Student类的对象数组stuArray,里面放置若干个学生对象,将数组中的数据依次写入文件stu.dat;然后读取该文件中的信息,依次还原出数组中各Student对象的信息。
法1:
package etc.sdut.edu.cn;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//创建Student类
class Student{
String id ; //学号
String name; // 姓名
int age ; //年龄
String classes ; //班级
public Student(String id, String name, int age, String classes) {
super();
this.id = id;
this.name = name;
this.age = age;
this.classes = classes;
}
//重写toString方法
@Override
public String toString() {
return "学生 学号=" + id + ", 姓名=" + name + ", 年龄=" + age + ", 班级=" + classes ;
}
}
//测试类
public class Test2 {
public static void main(String[] args) throws IOException {
//构建若干个学生对象
Student stuArray[] = {
new Student("111","lch",10,"rj1"),
new Student("112","ssh",10,"rj1"),
new Student("113","zzc",10,"rj1"),
new Student("114","lgw",10,"rj1"),
new Student("115","lss",10,"rj1"),
new Student("116","zyp",10,"rj1")
};
//创建一个字符输入流
FileWriter fw = new FileWriter(".\\src\\stu.dat") ;
//依次将学生对象读入指定文件
for(int i= 0 ; i<stuArray.length;i++)
{
fw.write(stuArray[i].toString());
fw.write("\n");//为了存储和输出的美观,每次读入数据后都加入一个换行符
}
fw.close();
//创建一个字符输出流
FileReader fr = new FileReader(".\\src\\stu.dat") ;
//下面是输出数据的操作
int len = 0 ;
while((len = fr.read())!=-1)
{
System.out.print((char)len);
}
fr.close();
}
}
法2:
package etc.sdut.edu.cn;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
//创建Student类
class Student1 implements Serializable{
int id ;
String name ;
int age;
String classs;
public Student1(int id, String name, int age, String classs) {
super();
this.id = id;
this.name = name;
this.age = age;
this.classs = classs;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", classs=" + classs + "]";
}
}
public class Text11 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建输入输出流
ObjectOutputStream dos = new ObjectOutputStream(new FileOutputStream(".\\src\\stut.dat")) ;
Student1[] s = new Student1[]{
new Student1(1,"s",1,"ajl"),
new Student1(2,"x",2,"akf"),
new Student1(2,"xs",3,"akf")
} ;
dos.writeObject(s);
dos.close();
ObjectInputStream dis = new ObjectInputStream(new FileInputStream(".\\src\\stut.dat")) ;
Student1 t[] = (Student1[])dis.readObject();
for(Student1 s1 : t)
{
System.out.println(s1);
}
dis.close();
}
}
第三题
3、用打开文本对话框选取文件,读写文件内容。
用打开文本对话框选取文件,读取文件内容,在显示器中显示出来,进行小写字母转换成大写字母,然后用保存文本对话框选取目标文件,向目标文件里面追加写入处理后的字符。
提示程序:
import java.io.*;
import javax.swing.*;
public class Main{
public static void main(String[] args) {
File file = null;
JFileChooser fc = new JFileChooser(); // 创建一个文件选择器
//打开文件,获得选择的文件名称
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {// 如果选择了确定按钮,就获得选择的文件对象
file = fc.getSelectedFile(); //file存放选取的文件对象
}
// 创建输入流,读取文件,对文件内容进行处理
//…………………………
//处理完毕
//保存文件
if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {// 如果选择了确定按钮,就获得选择的文件对象
file = fc.getSelectedFile(); //file存放选取的文件对象
}
//创建输出流,写文件内容
}
}
package etc.sdut.edu.cn;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
public class Test3 {
public static void main(String[] args) throws IOException {
File file = null;
JFileChooser fc = new JFileChooser(); // 创建一个文件选择器
//打开文件,获得选择的文件名称
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {// 如果选择了确定按钮,就获得选择的文件对象
file = fc.getSelectedFile(); //file存放选取的文件对象
}
// 创建输入流,读取文件,对文件内容进行处理
BufferedReader br = new BufferedReader(new FileReader(file));
//在显示屏显示原数据,并进行大小写转化
int len = 0 ;
String str1 = "" ; //用来记录大小写转化后的序列
String str2 = "" ; //用来记录开始的数据
char ch[] = new char[100] ;
while((len=br.read(ch))!=-1)
{
String str = new String(ch,0,len) ;
System.out.println(str);//在显示屏上显示原字符
str1+=str.toUpperCase() ;
str2+=str;
}
br.close();
if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {// 如果选择了确定按钮,就获得选择的文件对象
file = fc.getSelectedFile(); //file存放选取的文件对象
}
//创建输出流,写文件内容
BufferedWriter bw = new BufferedWriter(new FileWriter(file)) ;
bw.write(str2+str1);//在原数据上追加改变之后的数据
bw.close();
}
}
第四题
4、读取网络资源,写出代码至文件。
读取“http://www.sdut.edu.cn"网页内容,存至“sdut.html"。
提示语句:
URL url=new URL("http://www.sdut.edu.cn");
InputStream is = url.openStream();
package etc.sdut.edu.cn;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class Test4 {
public static void main(String[] args) throws IOException {
//获取网页内容
URL url = new URL("https://www.sdut.edu.cn") ;
InputStream is = url.openStream();
//创建输出流,写文件内容
BufferedInputStream bs = new BufferedInputStream(is) ;
BufferedOutputStream bw =new BufferedOutputStream(new FileOutputStream (new File(".\\src\\sdut.html"))) ;
int d = 0 ;
while((d = bs.read())!=-1)
{
bw.write(d);
}
bw.close();
bs.close();
is.close();
}
}
第五题
5、利用各种输入输出流进行读写文件,分析其读写效率。
读出文件D:\test1.txt的内容,显示在屏幕的同时,将文件写至D:\test2.txt中,计算其执行的时间,从而分析执行的效率。
提示,可用如下的输入/输入流类:
字节输入/输出流:FileInputStream和FileOutputStream、BufferedInputStream和BufferedOutputStream、DataInputStream和DataOutputStream;
字符输入/输出流:FileReader和FileWriter、BufferedReader 和 BufferedWriter
package etc.sdut.edu.cn;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test5 {
public static void main(String[] args) throws IOException {
//创建输入流和输出流
BufferedReader br = new BufferedReader(new FileReader(".\\src\\test1.txt")) ;
BufferedWriter bw = new BufferedWriter(new FileWriter(".\\src\\test2.txt")) ;
long t1 = System.currentTimeMillis();//记录开始时间
int d = 0 ;
//下面进行读取文件和写入文件
while((d = br.read())!=-1)
{
System.out.print((char)d);
bw.write(d);
}
System.out.println();
br.close();
bw.close();
//记录程序结束时间
long t2 = System.currentTimeMillis() ;
System.out.println(t2-t1);//计算出其执行效率
}
}
第六题
6、利用DataInputStream和DataOutputStream读写文件。
将数字1-100、字母A-Z、a—z写出至文件D:\abc.txt中,然后读取出来,显示在屏幕上。
package etc.sdut.edu.cn;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test6 {
public static void main(String[] args) throws IOException {
//创建输出流
DataOutputStream dos = new DataOutputStream(new FileOutputStream(".\\abc.txt"));
String str ="1" ; //记录文本数据
//将1到100存入数据中
for(int i= 2 ; i<=100 ;i++)
{
str=str+" "+i ;
}
//将A到Z存入数据
for(int i = 65 ; i<=90;i++)
{
str=str+" "+(char)i;
}
//将a到z存入数据
for(int i= 97 ; i<=122 ; i++)
{
str=str+" "+(char)i;
}
//将字符串转化为字节数组写入流中
dos.write(str.getBytes());
dos.close();
//创建输入流
DataInputStream dis = new DataInputStream(new FileInputStream(".\\abc.txt")) ;
//在显示屏上输出
int len = 0 ;
byte b[] = new byte[21] ;
while((len = dis.read(b))!=-1)
{
System.out.println(new String(b,0,len));
}
dis.close();
}
}
第七题
7、利用File类进行文件及文件夹名称的显示。
编写应用程序,列出指定目录(d:/test)下的所有文件和目录名,然后将该目录下的所有文件后缀名为.txt的文件过滤出来显示在屏幕上。
package etc.sdut.edu.cn;
import java.io.File;
import java.io.FilenameFilter;
public class Test7 {
public static void main(String[] args) {
//创建一个File类
File file = new File("d:/test") ;//这是我原来用来测试的地址.\\src
//用一个String类型来存储该目录下文件和文件名
String[] list = file.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if(name.endsWith(".txt"))
return true ;
else
return false;
}
});
//在显示屏上显示
if(list!=null) {
for(String s: list)
{
System.out.println(s);
}
}
}
}
第八题
8、使用File类创建一个多层目录d:\java\ch10\src。
package etc.sdut.edu.cn;
import java.io.File;
public class Test8 {
public static void main(String[] args) {
File file = new File("d:\\java\\ch10\\src") ; //.\\src\\java\\ch10\\src
//如果file这个目录不存在,那么就创建目录
if(!file.exists())
{
file.mkdirs();
}
}
}
第九题
9、使用File类列出某一个目录下创建日期晚于2017-08-10的文件。
package etc.sdut.edu.cn;
import java.io.File;
import java.io.FileFilter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test9 {
public static void main(String[] args) throws ParseException {
//首先,先创建一个时间日期2017-08-10
String str = "2017-08-10" ;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") ;
Date date = sdf.parse(str) ;
//读取文件目录
File file = new File(".\\src") ;
//然后,将该目录下的文件都存放入list数组中
File[] list = file.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
if(pathname.isFile())
return true ;
else
return false;
}
});
for(File l:list)
{
//将文件的最后的修改时间求出和设定时间进行比较
long t = l.lastModified();
Date date1 = new Date(t) ;
//如果最后一次修改时间在设定时间之后,就输出这个文件
if(date1.after(date))
{
System.out.println(l.getName());
}
}
}
}
第十题
10、利用File类编程完成如下任务:
(1)列出一个目录下的所有文件和目录名称;
(2)列出一个目录及子目录下的所有文件名称;
(3)列举一个目录下的所有.java文件名称;
(4)列举一个目录及其子目录下的所有.java文件名称。
package etc.sdut.edu.cn;
import java.io.File;
import java.io.FilenameFilter;
//建一个对文件和目录进行操作的操作类
class FileTool{
//列出一个目录下的所有文件和目录名称
public static void dir(File file)
{
if(file.isDirectory())
{
String[] list = file.list();
for(String s:list)
{
System.out.println(s);
}
}
//如果传入的不是目录之间输入这个文件的名称
else System.out.println(file.getName());
}
//列出一个目录及其子目录下的所有文件和目录名称
public static void dir1(File file)
{
//利用递归的思想直至全是文件
if(file.isDirectory())
{
File[] list = file.listFiles();
for(File s :list)
{
if(s.isFile())
{
System.out.println(s.getName());
}
else dir1(s) ;
}
}
else System.out.println(file.getName());
}
//列举一个目录下的所有.java文件名称
public static void dir2(File file, String name1)
{
if(file.isDirectory())
{
String[] list = file.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if(name.endsWith(name1))
return true;
else
return false;
}
});
for(String s :list)
{
System.out.println(s);
}
}
else
{
if(file.getName().endsWith(name1))
System.out.println(file.getName());
}
}
//列举一个目录及其子目录下的所有.java文件名称
public static void dir3(File file, String name1)
{
if(file.isDirectory())
{
File[] list = file.listFiles() ;
for(File s:list)
{
if(s.isDirectory())
{
dir3(s,name1) ;
}
else
{
if(s.getName().endsWith(name1))
System.out.println(s.getName());
}
}
}
else {
if(file.getName().endsWith(name1))
System.out.println(file.getName());
}
}
}
public class Test10 {
public static void main(String[] args) {
FileTool.dir(new File(".\\src"));
System.out.println("------------------------");
FileTool.dir1(new File(".\\src"));
System.out.println("------------------------");
FileTool.dir2(new File(".\\src"),".java");
System.out.println("------------------------");
FileTool.dir3(new File(".\\src"),".java");
}
}