2014/9/23

1
System.in

 1 public class wori                                    //先用InputStreamReader套System.in,再用BufferedReader套是常用方法
 2 {
 3     public static void main(String args[])
 4     {
 5         InputStreamReader a = new InputStreamReader(System.in);                // System.in是InputStream父类的引用,用于键盘输入
 6         BufferedReader b = new BufferedReader(a);                              
 7         String s = null;
 8         try
 9         {
10             while((s = b.readLine()) != null)                //使用BufferedReader的方法readLine()
11             {
12                 if(s.equalsIgnoreCase("exit"))                //
13                 {
14                     break;
15                 }
16                 System.out.println(s.toUpperCase());
17             }
18             b.close();                                                    //b close即可
19         }
20         catch(IOException e)
21         {
22             e.printStackTrace();
23         }
24     }
25 }

2
DataInputStream, DateOutputStream, ByteArrayInputStream, ByteArrayOutputStream 用于直接读写基本类型数据

 1 public class wori
 2 {
 3     public static void main(String args[])
 4     {
 5         ByteArrayOutputStream baos = new ByteArrayOutputStream();       //分配内存给一个字节数组,以此为节点
 6         DataOutputStream dos = new DataOutputStream(baos);              //
 7         try
 8         {
 9             dos.writeDouble(Math.random());                            //
10             ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());  //toByteArray()返回一个字节数组作为节点
11             DataInputStream dis = new DataInputStream(bais);
12             System.out.println(dis.readDouble());                   //
13             dos.close();
14             dis.close();
15         }
16         catch(IOException e)
17         {
18             System.exit(-1);
19         }
20         
21     }
22 }


3
PrintStream

 1 public class wori
 2 {
 3     public static void main(String args[])
 4     {
 5         PrintStream ps = null;
 6         try
 7         {
 8             FileOutputStream fos = new FileOutputStream("d:/1.txt");
 9             ps = new PrintStream(fos);
10         }
11         catch(IOException e)
12         {
13             System.exit(0);
14         }
15         System.setOut(ps);                               //
16         int i = 0;
17         while(i < 100)
18         {
19             System.out.println(i);
20             i++;
21         }
22         ps.close();
23     }
24 }

4

 1 public class wori
 2 {
 3     public static void main(String args[])
 4     {
 5         try{
 6         FileOutputStream a = new FileOutputStream("d:/1.txt");
 7         FileInputStream b = new FileInputStream("d:/1.txt");
 8         ObjectOutputStream oos = new ObjectOutputStream(a);
 9         ObjectInputStream ois = new ObjectInputStream(b);
10         Animal n = new Animal();
11         oos.writeObject(n);                        //
12         oos.flush();
13         oos.close();
14         Animal n1 = (Animal)ois.readObject();                       //
15         ois.close();
16         System.out.println(n1.a );
17         System.out.println( n1.k);
18         }
19         catch(IOException e)
20         {
21             
22         }
23         catch(ClassNotFoundException o)
24         {
25             
26         }
27         
28     }
29 }


class Animal implements Serializable //序列化接口
{
int a = 0;
transient int k = 23; //透明的,表示不能序列化,无法写进流中
}

转载于:https://www.cnblogs.com/melon2014/p/3990461.html

好的,根据您的要求,可以定义如下的三个类: ```c++ #include <iostream> using namespace std; class Date { public: Date(int y, int m, int d) { SetDate(y, m, d); } void SetDate(int y, int m, int d) { year = y; month = m; day = d; // 判断日期的正确性 if (month < 1 || month > 12) { cout << "Error: Invalid month!" << endl; exit(1); } if (day < 1 || day > GetMaxDay()) { cout << "Error: Invalid day!" << endl; exit(1); } } void print() { cout << year << "/" << month << "/" << day; } protected: int year, month, day; private: bool IsLeapYear() { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; } int GetMaxDay() { static const int max_day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month == 2 && IsLeapYear()) { return 29; } else { return max_day[month]; } } }; class Time { public: Time(int h, int m, int s) { SetTime(h, m, s); } void SetTime(int h, int m, int s) { hour = h; minute = m; second = s; // 判断时间的正确性 if (hour < 0 || hour > 23) { cout << "Error: Invalid hour!" << endl; exit(1); } if (minute < 0 || minute > 59) { cout << "Error: Invalid minute!" << endl; exit(1); } if (second < 0 || second > 59) { cout << "Error: Invalid second!" << endl; exit(1); } } void print() { cout << hour << ":" << minute << ":" << second; } protected: int hour, minute, second; }; class DateTime : public Date, public Time { public: DateTime(int ye, int mo, int da, int ho, int mi, int se) : Date(ye, mo, da), Time(ho, mi, se) {} void print() { Date::print(); cout << " "; Time::print(); } }; ``` 这个实现中,Date类和Time类都提供了构造函数和print()函数,其中Date类还提供了SetDate()函数,用于设置日期,Time类还提供了SetTime()函数,用于设置时间。DateTime类继承了Date类和Time类,并提供了自己的构造函数和print()函数,其中构造函数使用了初始化列表来初始化Date和Time的成员。在DateTime的print()函数中,我们分别调用了Date::print()和Time::print()函数,并在它们之间输出一个空格,从而实现了以“2014/12/9 18:21:00”的形式输出时间的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值