1、 填空题java
方法给程序提供了一个从输入流中读取数据的基本方法。设计模式
2、 选择题数组
1.网络 |
使用Java IO流实现对文本文件的读写过程当中,须要处理下列( B )异常。(选择一项)this |
|
|
|
|
|
A编码 |
ClassNotFoundExceptionspa |
|
B.设计 |
IOException代理 |
|
C.code |
SQLException |
|
D. |
RemoteException |
2. |
在Java的IO操做中,( D )方法能够用来刷新流的缓冲。(选择两项) |
|
|
|
|
|
A |
void release() |
|
B. |
void close() |
|
C. |
void remove() |
|
D. |
void flush() |
3. |
在Java中,下列关于读写文件的描述错误的是( B )。(选择一项) |
|
|
|
|
|
A |
Reader类的read()方法用来从源中读取一个字符的数据 |
|
B. |
Reader类的read(int n )方法用来从源中读取一个字符的数据 |
|
C. |
Writer类的write(int n)方法用来向输出流写入单个字符 |
|
D. |
Writer类的write(String str)方法用来向输出流写入一个字符串 |
4. |
阅读下列文件定入的Java代码,共有( C )处错误。(选择一项) |
|
|
import java.io.*; public class TestIO { public static void main(String []args){ String str ="文件写入练习"; FileWriter fw = null; //1 try{ fw = new FileWriter("c:\mytext.txt"); //2 fw.writerToEnd(str); //3 }catch(IOException e){ //4 e.printStackTrace(); }finally{ //此处省略关闭流 } } } |
|
|
|
|
|
A |
0 |
|
B. |
1 |
|
C. |
2 |
|
D. |
3 |
5. |
分析以下Java代码,有标注的四行代码中,有错误的是第( D )处。(选择一项) |
|
|
import java.io.FileWriter; import java.io.IOException; public class Test { public static void main(String[ ] args) { String str = "Hello World"; FileWriter fw = null; try { fw = new FileWriter("c:\\hello.txt"); // 1 fw.write(str); // 2 } catch (IOException e) { e.printStackTrace(); // 3 } finally { fw.close(); // 4 } } } |
|
|
|
|
|
A |
1 |
|
B. |
2 |
|
C. |
3 |
|
D. |
4 |
6. |
如下选项中关于以下代码的说法正确的是( AD )。(选择二项) |
|
|
public class TestBuffered { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("d:/bjsxt1.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("d:/bjsxt2.txt")); String str = br.readLine(); while(str !=null){ bw.write(str); bw.newLine(); str = br.readLine(); } br.close(); bw.close(); } } |
|
|
|
|
|
A. |
该类使用字符流实现了文件复制,将d:/bjsxt1.txt复制为d:/bjsxt2.txt |
|
B. |
FileReader和FileWriter是处理流,直接从文件读写数据 |
|
C. |
BufferedReader和BufferedWriter是节点流,提供缓冲区功能,提升读写效率 |
|
D. |
readLine()能够读取一行数据,返回值是字符串类型,简化了操做 |
7. |
InputStreamReader是转换流,能够将字节流转换成字符流,是字符流与字节流之间的桥梁。它的实现使用的设计模式是( C )。(选择一项) |
|
|
|
|
|
A. |
工厂模式 |
|
B. |
装饰模式 |
|
C. |
适配器模式 |
|
D. |
代理模式 |
3、 判断题
4、 简答题
5、 编码题
1.实现字符串和字节数组之间的相互转换。必如将字符串“北京尚学堂bjsxt”转换为字节数组,并将字节数组再转换回字符串。
1 public class TestConvert 2 { 3 public static void main(String[] args) throws IOException 4 { 5 //准备一个字符串
6 String contents = " 近日,北京尚学堂科技有限公司正式成为央视网广告合做伙伴"; 7 System.out.println(contents); 8 //String---byte []
9 byte[] buf = contents.getBytes(); 10 //byte[]----String
11 String contents2 = new String(buf, 0, buf.length); 12 System.out.println(contents2); 13 } 14 }
2.实现字节数组和任何基本类型和引用类型执行的相互转换
提示:使用ByteArrayInutStream和ByteArrayOutputStream。
1 public class TestByteArrayStream 2 { 3 public static void main(String[] args) throws IOException, 4 ClassNotFoundException 5 { 6 int num = 50; 7 boolean flag = true; 8 User user = new User("bjsxt", "bjsxt"); 9 //使用数据包把数据封装起来 10 //各类数据类型----->byte[] ByteArrayOutputStream
11 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 12 ObjectOutputStream oos = new ObjectOutputStream(baos);//包装流
13 oos.writeInt(num); 14 oos.writeBoolean(flag); 15 oos.writeObject(user); 16 byte[] buf = baos.toByteArray(); 17 baos.close(); 18 //byte[]----------->各类数据类型
19 ByteArrayInputStream bais = new ByteArrayInputStream(buf); 20 ObjectInputStream ois = new ObjectInputStream(bais); 21 int num2 = ois.readInt(); 22 boolean flag2 = ois.readBoolean(); 23 User user2 = (User) ois.readObject(); 24 System.out.println(num2); 25 System.out.println(flag2); 26 System.out.println(user2); 27 bais.close(); 28 } 29 }
3.分别使用文件流和缓冲流复制一个长度大于100MB的视频文件,并观察效率的差别。
1 public class TestCopy4 2 { 3 public static void main(String[] args) throws IOException 4 { 5 //建立输入流和输出流
6 InputStream fis = new FileInputStream(new File("d:/1.mp4")); 7 OutputStream fos = new FileOutputStream("d:/2.mp4"); 8 //使用输入流和输出流复制文件
9 byte[] buf = new byte[10]; 10 int len = fis.read(buf); 11 while (len != -1) 12 { 13 //写
14 fos.write(buf, 0, len); 15 //读
16 len = fis.read(buf); 17 //System.out.println(len);
18 } 19 //关闭输入流和输出流
20 fis.close(); 21 fos.close(); 22 } 23 } 24 public class TestCopy 25 { 26 public static void main(String[] args) throws IOException 27 { 28 //建立输入流和输出流
29 InputStream fis = new FileInputStream(new File("d:/1.mp4")); 30 OutputStream fos = new FileOutputStream("d:/2.mp4"); 31 BufferedInputStream bis = new BufferedInputStream(fis); 32 BufferedOutputStream bos = new BufferedOutputStream(fos); 33 //使用输入流和输出流复制文件
34 byte[] buf = new byte[10]; 35 int len = bis.read(buf); 36 while (len != -1) 37 { 38 //写
39 bos.write(buf, 0, len); 40 //读
41 len = bis.read(buf); 42 } 43 //关闭输入流和输出流
44 bis.close(); 45 bos.close(); 46 } 47 }
4.复制文件夹d:/sxtjava下面全部文件和子文件夹内容到d:/sxtjava2。
提示:涉及单个文件复制、目录的建立、递归的使用
1 public class CopyDir 2 { 3 /**
4 * 5 * 复制单个文件 6 * 7 * @param sourceFile 源文件 8 * 9 * @param targetFile 目标文件 10 * 11 * @throws IOException 12 * 13 */
14 public static void copyFile(File sourceFile, File targetFile) throws IOException 15 { 16 BufferedInputStream inBuff = null; 17 BufferedOutputStream outBuff = null; 18 try
19 { 20 // 新建文件输入流
21 inBuff = new BufferedInputStream(new FileInputStream(sourceFile)); 22 // 新建文件输出流
23 outBuff = new BufferedOutputStream(new FileOutputStream(targetFile)); 24 // 缓冲数组
25 byte[] b = new byte[1024 * 5]; 26 int len; 27 while ((len = inBuff.read(b)) != -1) 28 { 29 outBuff.write(b, 0, len); 30 } 31 // 刷新此缓冲的输出流
32 outBuff.flush(); 33 } finally
34 { 35 // 关闭流
36 if (inBuff != null) 37 { 38 inBuff.close(); 39 } 40 if (outBuff != null) 41 { 42 outBuff.close(); 43 } 44 } 45 } 46 /**
47 * 48 * 复制目录 49 * 50 * @param sourceDir 源目录 51 * 52 * @param targetDir 目标目录 53 * 54 * @throws IOException 55 * 56 */
57 public static void copyDirectiory(String sourceDir, String targetDir) 58 throws IOException 59 { 60 // 检查源目录
61 File fSourceDir = new File(sourceDir); 62 if (!fSourceDir.exists() || !fSourceDir.isDirectory()) 63 { 64 System.out.println("源目录不存在"); 65 return; 66 } 67 //检查目标目录,如不存在则建立
68 File fTargetDir = new File(targetDir); 69 if (!fTargetDir.exists()) 70 { 71 fTargetDir.mkdirs(); 72 } 73 // 遍历源目录下的文件或目录
74 File[] file = fSourceDir.listFiles(); 75 for (int i = 0; i < file.length; i++) 76 { 77 if (file[i].isFile()) 78 { 79 // 源文件
80 File sourceFile = file[i]; 81 // 目标文件
82 File targetFile = new File(fTargetDir, file[i].getName()); 83 copyFile(sourceFile, targetFile); 84 } 85 //递归复制子目录
86 if (file[i].isDirectory()) 87 { 88 // 准备复制的源文件夹
89 String subSourceDir = sourceDir + File.separator + file[i].getName(); 90 // 准备复制的目标文件夹
91 String subTargetDir = targetDir + File.separator + file[i].getName(); 92 // 复制子目录
93 copyDirectiory(subSourceDir, subTargetDir); 94 } 95 } 96 } 97 public static void main(String[] args) throws IOException 98 { 99 copyDirectiory("d:/sxtjava", "d:/sxtjava2"); 100 } 101 }
可选题
1.使用IO包中的类读取D盘上exam.txt文本文件的内容,每次读取一行内容,将每行做为一个输入放入ArrayList的泛型集合中并将集合中的内容使用增强for进行输出显示。
1 public class Test 2 { 3 public static void main(String[] args) throws IOException 4 { 5 String path = "D:\\exam.txt"; 6 outputMethod(path); 7 } 8 public static void outputMethod(String path) throws IOException 9 { 10 List<String> list = new ArrayList<String>(); // 建立集合对象 11 // 建立缓冲区对象
12 BufferedReader br = new BufferedReader(new FileReader(path)); 13 String line = br.readLine(); // 读取数据每次读一行
14 while (line != null) 15 { 16 list.add(line); 17 line = br.readLine(); 18 } 19 br.close(); //关闭
20 for (String s : list) 21 { 22 System.out.println(s); 23 } 24 } 25 }
2.假设从入学开始全部书写的Java类代码都在d:/sxtjava文件夹下,包括多级子文件夹。使用IO流获取从入学开始,到目前为止已经写了多少行Java代码。
提示:
其实就是获取d:/sxtjava文件夹及其子文件夹下的全部.java文件,使用readLine()读取其中每一行,每读取一行,行数加1。全部的文件读取完毕,获得总共已经写的Java代码行数。须要结合递归实现。
1 public class TestCountDir 2 { 3 private int count; 4 /**
5 * 6 * 统计一个java文件的行数 7 * 8 */
9 private void countLine(File sourceFile) throws IOException 10 { 11 BufferedReader br = null; 12 try
13 { 14 // 新建文件输入流
15 br = new BufferedReader(new FileReader(sourceFile)); 16 while (br.readLine() != null) 17 { 18 count++; 19 //System.out.println(count);
20 } 21 } finally
22 { 23 br.close(); 24 } 25 } 26 /**
27 * 28 * 统计一个目录下全部Java文件的行数 29 * 30 */
31 private void countDir(String sourceDir) throws IOException 32 { 33 // 检查源目录
34 File fSourceDir = new File(sourceDir); 35 if (!fSourceDir.exists() || !fSourceDir.isDirectory()) 36 { 37 System.out.println("源目录不存在"); 38 return; 39 } 40 // 遍历目录下的文件或目录
41 File[] file = fSourceDir.listFiles(); 42 for (int i = 0; i < file.length; i++) 43 { 44 if (file[i].isFile()) 45 { 46 if (file[i].getName().toLowerCase().endsWith(".java")) 47 { 48 // System.out.println(file[i].getName());
49 countLine(file[i]); 50 } 51 } 52 //递归统计代码行数
53 if (file[i].isDirectory()) 54 { 55 // 准备统计的文件夹
56 String subSourceDir = sourceDir + File.separator + file[i].getName(); 57 // 统计子目录
58 countDir(subSourceDir); 59 } 60 } 61 } 62 public static void main(String[] args) throws IOException 63 { 64 TestCountDir tcd = new TestCountDir(); 65 tcd.countDir("d:/sxtjava"); 66 System.out.println(tcd.count); 67 } 68 }
3.由控制台按照固定格式输入学生信息,包括学号,姓名,年龄信息,当输入的内容为exit退出;将输入的学生信息分别封装到一个Student对象中,再将每一个Student对象加入到一个集合中,要求集合中的元素按照年龄大小正序排序;最后遍历集合,将集合中学生信息写入到记事本,每一个学生数据占单独一行。
1 public class Student implements Comparable<Student>
2 { 3 private Integer num; 4 private String name; 5 private Integer age; 6 //省略getter和setter方法 7 //省略构造方法
8 public int compareTo(Student stu) 9 { 10 return this.age - stu.age; 11 } 12 public String toString() 13 { 14 return "Student [age=" + age + ", name=" + name 15 + ", num=" + num + "]"; 16 } 17 } 18 public class Test 19 { 20 public static void main(String[] args) 21 { 22 Set<Student> stuSet = saveStudentInfo(); 23 outputInfo(stuSet); 24 } 25 private static Set<Student> saveStudentInfo() 26 { 27 Scanner input = new Scanner(System.in); 28 // 保存学生信息的TreeSet集合对象
29 Set<Student> stuSet = new TreeSet<Student>(); 30 while (true) 31 { 32 // 输入提示
33 System.out.println("请输入学生信息:(学号#姓名#年龄)"); 34 String inputData = input.nextLine(); 35 // 判断是否退出 inputData.equals("exit")
36 if ("exit".equals(inputData)) 37 { 38 break; 39 } 40 // 将用户输入的学生信息分割为String[]
41 String[] info = inputData.split("#"); 42 // 将输入信息封装到Student对象中
43 Student stu 44 = new Student(Integer.parseInt(info[0]), info[1], 45 Integer.parseInt(info[2])); 46 // 将学生对象加入集合
47 stuSet.add(stu); 48 } 49 return stuSet; 50 } 51 private static void outputInfo(Set<Student> stuSet) 52 { 53 File file = new File("e:/student.txt"); 54 // 建立文件输出流对象
55 FileWriter fw = null; 56 try
57 { 58 fw = new FileWriter(file); 59 Iterator<Student> it = stuSet.iterator(); 60 while (it.hasNext()) 61 { 62 String info = it.next().toString(); 63 // 将info字符串,写入记事本
64 fw.write(info); 65 // 完成换行功能
66 fw.write("\r\n"); 67 } 68 } catch (Exception e) 69 { 70 e.printStackTrace(); 71 } finally
72 { 73 try
74 { 75 fw.close(); 76 } catch (IOException e) 77 { 78 e.printStackTrace(); 79 } 80 } 81 } 82 }