输入输出机制
Posted timhliu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了输入输出机制相关的知识,希望对你有一定的参考价值。
1.递归遍历目录,显示其中的文件名和目录名。若为文件,则直接显示文件的大小;若为目录,则显示目录中的文件和子目录。注意显示文件大小的单位(KB或MB)
1 package 目录文件信息;
2
3 import java.io.File;
4
5 public class FileList {
6 public static void main(String[] args) {
7 FileList fileList = new FileList();
8 File file = new File("E:"+File.separator+"test");//默认分隔符
9 fileList.list(file);
10 }
11
12 //递归
13 public void list(File file){
14 printName(file);
15 if(file.isDirectory()){
16 File[] list = file.listFiles();
17 for (File file2 : list) {
18 list(file2);
19 }
20 }
21 }
22
23 public void printName(File file){
24 String name = file.getName();
25 if(file.isDirectory())
26 System.out.println("Dir: "+name);
27 else
28 System.out.println(file.getName()+" "+(file.length()/1024f)+"kb");
29 }
30
31 }
2.选择输入流和输出流,实现目录拷贝
1 package 目录拷贝;
2
3 import java.io.*;
4
5 class FileTest {
6 public void copyFolder(String oldPath, String newPath) {
7 try {
8 (new File(newPath)).mkdirs();// 如果文件夹不存在 则建立新文件夹
9 File filelist = new File(oldPath);//读取整个文件夹的内容到file字符串数组,下面设置一个游标i,不停地向下移开始读这个数组
10 String[] file = filelist.list();
11 File temp = null;
12 for (int i = 0; i < file.length; i++) {
13 //如果以分隔符结尾
14 if (oldPath.endsWith(File.separator)) {
15 temp = new File(oldPath + file[i]);
16 } else {
17 temp = new File(oldPath + File.separator + file[i]);
18 }
19
20 //遇到文件
21 if (temp.isFile()) {
22 FileInputStream input = new FileInputStream(temp);
23 FileOutputStream output = new FileOutputStream(newPath+ "/" + "new_" + (temp.getName()).toString());
24 byte[] bufferarray = new byte[1024 * 64];
25 int prereadlength;
26 //一次读出1024字节,循环读出
27 while ((prereadlength = input.read(bufferarray))!=-1) {
28 output.write(bufferarray, 0, prereadlength);
29 }
30 output.flush();//刷新方法,读出最后一个字节
31 output.close();
32 input.close();
33 }
34 //遇到文件夹,递归
35 if (temp.isDirectory()) {
36 copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
37 }
38 }
39 } catch (Exception e) {
40 System.out.println("复制整个文件夹内容操作出错");
41 }
42 }
43 }
44 public class FileCopy {
45 public static void main(String args[]) {
46 new FileTest().copyFolder("e:/a", "e:/b");
47
48 }
49 }
3.从控制台输入Student类的信息,包括学号、姓名、年龄,如输入错误,提示用户重新输入,实现保存对象到文件和从文件读出对象并显示
1 package Test;
2
3 import java.io.*;
4 import java.util.*;
5
6 public class Student implements Serializable{
7 //如果没有指定serialVersionUID,系统会自动生成一个
8 private static final long serialVersionUID = 1L;
9 private String ID;
10 private String name;
11 private int age;
12 private static Scanner sc=new Scanner(System.in);
13 public Student(String iD, String name, int sge) {
14 super();
15 this.ID = iD;
16 this.name = name;
17 this.age = sge;
18 }
19
20 public Student() {
21 super();
22 System.out.println("请输入学生学号:");
23 while(true) {
24 try {
25 ID = sc.nextLine();
26 if(ID!=null&&ID.matches("^[0-9]+$")) {
27 System.out.println("输入合法");
28 break;
29 }else {
30 throw new Exception();
31 }
32 }catch(Exception e){
33 System.out.println("输入非法,请重新输入:");
34 }
35 }
36 System.out.println("请输入学生姓名:");
37 while(true) {
38 try {
39 name = sc.nextLine();
40 if(name!=null&&name.matches("^([\u4e00-\u9fa5]){2,7}$")) {
41 System.out.println("输入合法");
42 break;
43 }else {
44 throw new Exception();
45 }
46 }catch(Exception e){
47 System.out.println("输入非法,请重新输入:");
48 }
49 }
50 System.out.println("请输入学生年龄:");
51 while(true) {
52 try {
53 age = sc.nextInt();
54 String Age=String.valueOf(age);
55 if(age<0||age>50) {
56 throw new ArithmeticException();
57 }
58 if(Age!=null&&Age.matches("^[0-9]+$")) {
59 System.out.println("输入合法");
60 break;
61 }else{
62 throw new NumberFormatException();
63 }
64 }catch(ArithmeticException e){
65 System.out.println("输入非法,请重新输入:");
66 }catch(NumberFormatException e) {
67 System.out.println("输入非法,请重新输入:");
68 }catch(InputMismatchException e) {
69 sc.nextLine();
70 System.out.println("输入非法,请重新输入:");
71 }
72 }
73 }
74 public void show() {
75 System.out.println("学号:"+ID);
76 System.out.println("姓名:"+name);
77 System.out.println("年龄:"+age);
78 }
79 public String toString() {
80 return "学号"+ID+" "+"姓名"+name+" "+"年龄"+age;
81 }
82 }
1 package Test;
2
3 import java.io.*;
4
5 public class TestStudent{
6 public static void main(String[] args) throws Exception {
7 System.out.println("请输入学生信息");
8 Student stu=new Student();
9 writeObjectToFile(stu);
10 stu.show();
11 readObjectFromFile();
12 }
13 public static void writeObjectToFile (Object obj)throws IOException{
14 File file =new File("e:/1.txt");
15 FileOutputStream out;
16 try {
17 out = new FileOutputStream(file);
18 ObjectOutputStream objOut=new ObjectOutputStream(out);//将对象的状态保存在内存中
19 objOut.writeObject(obj);
20 objOut.flush();
21 objOut.close();
22 System.out.println("写入成功!");
23 } catch (IOException e) {
24 System.out.println("写入失败");
25 e.printStackTrace();
26 }
27 }
28
29 public static Object readObjectFromFile()throws IOException{
30 Object temp=null;
31 File file =new File("e:/1.txt");
32 FileInputStream in;
33 try {
34 in = new FileInputStream(file);
35 ObjectInputStream objIn=new ObjectInputStream(in);
36 temp=objIn.readObject();
37 objIn.close();
38 System.out.println("读入成功!");
39 } catch (IOException e) {
40 System.out.println("读入失败");
41 e.printStackTrace();
42 } catch (ClassNotFoundException e) {
43 e.printStackTrace();
44 }
45 System.out.println(temp.toString());
46 return temp;
47 }
48 }
以上是关于输入输出机制的主要内容,如果未能解决你的问题,请参考以下文章
[Go] 通过 17 个简短代码片段,切底弄懂 channel 基础