前言
本期任务:毕向东老师Java视频教程学习笔记(共计25天)
- 原视频链接:黑马程序员_毕向东_Java基础视频教程
- day01:编写HelloWorld程序
- day02:操作符与条件选择语句
- day03:循环语句与函数
- day04:数组
- day07:继承、抽象类与接口
- day08:多态
- day09:异常处理
- day11:多线程
- day12:线程安全与同步机制
- day13:String类
- day14:集合(ArrayList,LinkedList,HashSet)
- day15:集合(TreeSet)和泛型)
- day16:集合(HashMap、TreeMap)
- day17:集合框架的工具类(Arrays、Collections)
- day18:IO流(字符流读写)
- day19:IO流(字节流、转换流读写)
- day20:IO流(File对象)
代码
/*
字符流和字节流:
字节流两个基类:
InputStream OutputStream
字符流两个基类:
Reader Writer
先学习一下字符流的特点。
既然IO流是用于操作数据的,
那么数据的最常见体现形式是:文件。
那么先以操作文件为主来演示。
需求:在硬盘上,创建一个文件并写入一些文字数据。
找到一个专门用于操作文件的Writer子类对象。FileWriter。 后缀名是父类名。 前缀名是该流对象的功能。
*/
import java.io.*;
public class FileWriterDemo {
public static void main(String[] args) throws Exception{
//创建一个FileWriter对象。该对象一被初始化就必须要明确被操作的文件。
//而且该文件会被创建到指定目录下。如果该目录下已有同名文件,将被覆盖。
//其实该步就是在明确数据要存放的目的地。
FileWriter fw = new FileWriter("demo.txt");
// 调用write方法,将字符串写入到流中
fw.write("fsgd");
//刷新流对象中的缓冲中的数据。
//将数据刷到目的地中。
// fw.flush();
//关闭流资源,但是关闭之前会刷新一次内部的缓冲中的数据。
//将数据刷到目的地中。
//和flush区别:flush刷新后,流可以继续使用,close刷新后,会将流关闭。
fw.close();
}
}
/*
IO异常的处理方式。
*/
import java.io.*;
public class FileWriterDemo2 {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("demo.txt");
fw.write("dfalkjf;");
} catch (IOException e) {
System.out.println("catch: " + e.toString());
} finally {
try {
if (fw != null)
fw.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
}
/*
演示对已有文件的数据续写。
*/
import java.io.*;
public class FileWriterDemo3 {
public static void main(String[] args) throws IOException {
//传递一个true参数,代表不覆盖已有的文件。并在已有文件的末尾处进行数据续写。
FileWriter fw = new FileWriter("demo.txt", true);
fw.write("\r\n这是续写的部分内容。。。。\r\n");
fw.close();
}
}
import java.io.FileReader;
import java.io.IOException;
public class FileReaderDemo {
public static void main(String[] args)throws IOException {
//创建一个文件读取流对象,和指定名称的文件相关联。
//要保证该文件是已经存在的,如果不存在,会发生异常FileNotFoundException
FileReader fr = new FileReader("demo.txt");
//调用读取流对象的read方法。
//read():一次读一个字符。而且会自动往下读。若读取完毕,则返回-1
int ch = 0;
while ((ch=fr.read())!=-1){
System.out.println((char)ch);
}
fr.close();
}
}
import java.io.FileReader;
import java.io.IOException;
/*
第二种方式:通过字符数组进行读取。
*/
public class FileReaderDemo2 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("demo.txt");
//定义一个字符数组。用于存储读到字符。
//该read(char[])返回的是读到字符个数,文档读取完毕,则个数为-1。
char[] buf = new char[1024];
int num = 0;
while ((num = fr.read(buf)) != -1) {
System.out.println(new String(buf, 0, num));
}
fr.close();
}
}
//将C盘一个文本文件复制到D盘。
/*
复制的原理:
其实就是将C盘下的文件数据存储到D盘的一个文件中。
步骤:
1,在D盘创建一个文件。用于存储C盘文件中的数据。
2,定义读取流和C盘文件关联。
3,通过不断的读写完成数据存储。
4,关闭资源。
*/
import java.io.*;
public class CopyText {
public static void main(String[] args) throws Exception{
FileReader fr = new FileReader("demo.txt");
FileWriter fw = new FileWriter("demo_copy.txt");
char[] buf = new char[1024];
int num = 0;
while ((num=fr.read(buf))!=-1){
fw.write(buf, 0, num);
}
fr.close();
fw.close();
}
}
import java.util.*;
import java.text.*;
public class DateDemo {
public static void main(String[] args) {
Date d = new Date();
System.out.println(d);
// 将模式封装到SimpleDateFormat对象中,然后调用format方法让模式格式化指定Date对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 E kk:mm:ss");
System.out.println("time: "+sdf.format(d));
long l = System.currentTimeMillis();
System.out.println(new Date(l));
}
}
import java.util.*;
import java.text.*;
public class CalendarDemo {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
System.out.println(c);
String[] mons = {"1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"};
String[] weeks = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
int week = c.get(Calendar.DAY_OF_WEEK);
System.out.println(year + "年" + mons[month] + day + "日" + weeks[week]);
}
}
import java.util.*;
/*
两个练习:
1,获取任意年的二月有多少天。
思路:根据指定年设置一个时间就是
c.set(year,2,1)//某一年的3月1日。
c.add(Calenar.DAY_OF_MONTH,-1);//3月1日,往前推一天,就是2月最后一天。
2,获取昨天的现在这个时刻。
c.add(Calenar.DAY_OF_MONTH,-1);
*/
public class CalendarDemo2 {
public static void main(String[] args) {
// int year = 2020;
// int year = 2016;
// int year = 2000;
// int year = 1900;
// System.out.println(year + "年的二月有" + howManyDaysInFebruary(year) + "天");
curTimeYesterday();
}
public static int howManyDaysInFebruary(int year) {
Calendar c = Calendar.getInstance();
c.set(year, 2, 1);
c.add(Calendar.DAY_OF_MONTH, -1);
return c.get(Calendar.DAY_OF_MONTH);
}
public static void curTimeYesterday() {
Calendar c = Calendar.getInstance();
c.add(Calendar.DAY_OF_MONTH, -1);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR);
int min = c.get(Calendar.MINUTE);
int sec = c.get(Calendar.SECOND);
System.out.println("昨天的这个时候是:" + year + "年" + month + "月" + day + "日" + hour + "时" + min + "分" + sec + "秒");
}
}
/*
练习。给定一个小数。
保留该小数的后两位。
选作。可以考虑,保留时进行四舍五入。
思路:乘以保留的位数
*/
import java.util.*;
public class MathDemo {
public static void main(String[] args) {
// show();
saveTwo(12.3456, 2, true); // 预期输出12.35
}
public static void saveTwo(double d, int scale, boolean isRound) {
double base = Math.pow(10, scale);
double num = isRound ? Math.round(d * base) / base : (int) (d * base) / base;
System.out.println("num = " + num);
}
public static void show() {
// 返回值默认为double
System.out.println(Math.ceil(16.34)); // ceil返回大于指定数据的最小整数
System.out.println(Math.floor(12.34));// floor返回小于指定数据的最大整数
System.out.println(Math.round(12.34));// round实现四舍五入
System.out.println(Math.pow(2, 3));
System.out.println("..................................");
// 随机打印10个[1, 10]范围的整数
Random r = new Random();
for (int x = 0; x < 10; x++) {
System.out.println(r.nextInt(10) + 1);
}
}
}
/*
System:类中的方法和属性都是静态的。
out:标准输出,默认是控制台。
in:标准输入,默认是键盘。
描述系统一些信息。
获取系统属性信息:Properties getProperties();
//因为Properties是Hashtable的子类,也就是Map集合的一个子类对象。
//那么可以通过map的方法取出该集合中的元素。
//该集合中存储都是字符串。没有泛型定义。
*/
import java.util.*;
public class SystemDemo {
public static void main(String[] args) {
Properties prop = System.getProperties();
// 获取所有属性信息
for (Object obj: prop.keySet()){
System.out.println(obj+"::"+prop.get(obj));
}
// 在系统中自定义一些特有信息
System.setProperty("myKey", "myValue");
// 获取指定属性信息
System.out.println("os.name: "+ System.getProperty("os.name"));
System.out.println("myKey: "+ System.getProperty("myKey"));
}
}
/*
Runtime对象
该类并没有提供构造函数。
说明不可以new对象。那么会直接想到该类中的方法都是静态的。
发现该类中还有非静态方法。
说明该类肯定会提供了方法获取本类对象。而且该方法是静态的,并返回值类型是本类类型。
由这个特点可以看出该类使用了单例设计模式完成。
该方式是static Runtime getRuntime();
*/
public class RuntimeDemo {
public static void main(String[] args) throws Exception {
Runtime r = Runtime.getRuntime();
Process p = r.exec("notepad.exe RuntimeDemo.java");
Thread.sleep(4000);
p.destroy();
}
}