1、实验四 Java中的输入机制 实验日期: 2016 年 6 月 21 日 班级: 软件1401 学号(后四位):___0201______ 姓名:____焦小强_______ 成绩: 一. 实验目的 1 Java如何操作文件。 2 了解Java中的输入机制:如何从控制台输入,如何用InputStream和Reader显示文件中的内容。 二. 实验内容 编写能够满足如下条件的程序(1和2题选做一题,): 1 递归遍历目录,显示其中的文件名和目录名。若为文件,则直接显示
2、文件的大小;若为目录,则显示目录中的文件和子目录,并显示目录所含的所有文件的大小。注意显示文件大小的单位(KB或MB)。 2.列出指定目录下文件名包含某一字符串的文件。 package filer; import java.io.File; import java.io.FilenameFilter; public class FileExtFilter implements FilenameFilter { private String ext; public FileExtFilter(String ext) { this.ext = ext; }
3、 @Override public boolean accept(File dir, String name) { // TODO Auto-generated method stub if (new File(dir, name).isDirectory()) return false; if (name.contains(ext)) return true; return false; } } package filer; import java.io.File; public class FileList {
4、 public static void main(String[] args) { list(new File("D:\\空的\\akk"), "san"); } public static void list(File aFile, String ext) { if (!aFile.isDirectory()) return; String[] files = aFile.list(new FileExtFilter(ext)); if (files == null) return; for (String name : files)
5、 { System.out.println(name); } } } 3 用InputStream的子类读入一个英文文本文件,并用System.out显示其中的内容。 package readtxt; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class FileInputStreamDemo { public static void main(String[] args) throws IO
6、Exception { // TODO Auto-generated method stub String file = "D:\\空的\\apl\\yingwen.txt"; FileInputStreamDemo demo = new FileInputStreamDemo(); demo.readFile(file); } public void readFile(String fileName) throws IOException { InputStream in = null; try { in = new FileInput
7、Stream(fileName); int b = 0; while (true) { b = in.read(); if (b == -1) break; System.out.print((char) b); } } finally { if (in != null) { in.close(); } } } } 4 用Reader的子类读入一个一个字符文件,并用System.out显示其中的内容。 package readtxt; import java.io.F
8、ileReader; import java.io.IOException; import java.io.Reader; public class FileReaderDemo { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub String file = "D:\\空的\\apl\\zifu.txt"; FileReaderDemo demo = new FileReaderDemo(); demo.read
9、File(file); } public void readFile(String fileName) throws IOException { Reader reader = null; try { reader = new FileReader(fileName); int b = 0; while (true) { b = reader.read(); if (b == -1) break; System.out.print((char) b); } /* * 使用读入数组
10、的方式 reader=new FileReader(fileNmae); char[] c=new * char[8*1024]; int len=0; while((len=reader.read(c))!=-1){ * System.out.print(new String(c,0,len)); * */ } finally { if (reader != null) { reader.close(); } } } } 5 从控制台输入Student类的信息,包括学号、姓名、年龄,如输入错误,提
11、示用户重新输入。创建该类,并在toString方法中显示个人信息。 package StententInfo; public class studentsInfo { private long ID; private int Age; private String name; public void setID(long iD) { ID = iD; } public void setAge(int age) { Age = age; } public void setName(String name) { this.n
12、ame = name; } @Override public String toString() { return "studentsInfo [ID=" + ID + ", Age=" + Age + ", name=" + name + "]"; } } package StententInfo; import java.util.Scanner; public class testInput { public static void main(String[] args) { // TODO Auto-generated m
13、ethod stub studentsInfo stu = new studentsInfo(); Scanner scanner = new Scanner(System.in); System.out.println("请输入学号:"); if (scanner.hasNextLong()) { long iD = scanner.nextLong(); stu.setID(iD); } System.out.println("请输入姓名:"); if (scanner.hasNext()) { String name = scanner.next(); stu.setName(name); } System.out.println("请输入年龄:"); if (scanner.hasNextInt()) { int age = scanner.nextInt(); stu.setAge(age); } System.out.println(stu); } }






