------- android培训、java培训、java学习型技术博客、期待与您交流! ----------
知识点
01)System
/*
* System:类中的方法和属性都是静态的。
* out:标准输出,默认是控制台。
* in:标准输入,默认是键盘。
*/
public class SystemDemo {
public static void sop(Object obj){
System.out.println(obj);
}
public static void main(String[] args) {
method();
}
public static void method(){
/*
* 因为Properties是Hashtable的子类,也就是Map集合的一个子类对象。
* 那么可以通过map的方法取出该集合中的元素。
* 该集合中存储都是字符串。没有泛型定义。
*/
Properties prop = System.getProperties();
/*在系统中自定义一些特有信息。
* 通过System.setProperty(T, V)方法设置。
//System.setProperty("hah", "hhahhah");
*/
/*
* 获取指定属性信息。
* 通过System.getProperty(T)方法。
*/
String value = System.getProperty("os.name");//获取本机操作系统。
sop("value=" + value);
/*
* 如何在jvm启动时,动态加载一些属性信息?
* 通过命令行方式:java -Dxxx=qqq 类名 这个格式。xxx对应键,qqq对应值。
*/
/*
//获取所有属性信息。
for (Object obj : prop.keySet()){
String value = (String)prop.get(obj);
sop(obj + " = " + value);
}
*/
}
}
运行结果如下图所示:

02)Runtime
/*
* Runtime对象。
* 该类并没有提供构造函数:说明不可以new对象。那么会想到该类中都是些静态方法。
* 发现该类中还有非静态方法:说明该类肯定会提供了方法获取本类对象。而且该方法是静态的,并返回值类型是本类类型。
* 该方法是static Runtime getRuntime();
*/
public class RuntimeDemo {
public static void main(String[] args) throws IOException, InterruptedException{
method();
}
public static void method() throws IOException, InterruptedException{
Runtime r = Runtime.getRuntime();//获取本类对象。
Process p = r.exec("QQProtect.exe");//可执行程序放在:Windows系统文件System32文件之内。
//如果可执行程序不存在,运行后就会显示“Cannot run program”的提示信息,“找不到程序”。
Thread.sleep(1000);//睡眠1000毫秒。
p.destroy();//强制终止此 Process 对象表示的子进程。
}
}
运行结果如下图所示:

03)Date
/*
* 打印日期。
*/
public class DateDemo {
public static void sop(Object obj){
System.out.println(obj);
}
public static void main(String[] args) {
method();
}
public static void method(){
Date d = new Date();
sop("本地日期格式:" + d);//打印时间。
//将模式封装到SimpleDateformat对象中。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日E kk:mm:ss");
//调用format方法让模式格式化指定Date对象。
sop("格式化后格式:" + sdf.format(d));
}
}
运行结果如下图所示:

07)IO流(FileWriter)
/*
* 字符流和字节流:
* 字节流两个基类:InputStream OutputStream
*
* 字符流两个基类:Reader Writer
* 字符流的特点:IO流是用于操作数据的,数据的最常见体现形式是:文件。
*
* 以操作文件为主来演示。
* 需求:在硬盘上,创建一个文件并写入一些文字数据。
* 找到一个专门用于操作文件的Writer子类对象。FileWriter。 后缀名是父类名。 前缀名是该流对象的功能。
*
* void write(String str):写入字符串。
* abstract void flush():刷新该流的缓冲。IOException - 如果发生 I/O 错误
* abstract void close():关闭此流,但要先刷新它。
*/
public class FileWriterDemo {
public static void main(String[] args) throws IOException{
method();
}
public static void method() throws IOException{
/*
* 1)创建一个FileWriter对象。该对象一被初始化就必须要明确被操作的文件。
* 而且该文件会被创建到指定目录下。如果该目录下已有同名文件,将被覆盖。
* 其实该步就是在明确数据要存放的目的地。
*/
FileWriter fw = new FileWriter("WriterDemo.txt");
//2)调用write方法,将字符串写入到流中。
fw.write("aaaaa");
//3)刷新流对象缓冲区的数据,刷新到目的地中。
// fw.flush();
fw.write("bbbbb");
fw.write("ccccc");
fw.write("ddddd");
/*
* 04)(close)关闭流资源,但是关闭之前会刷新一次内部的缓冲中的数据。
* 将数据刷到目的地中。
* 和flush区别:flush刷新后,流可以继续使用,close刷新后,会将流关闭。
* 关闭此流,但要先刷新它。在关闭该流之后,再调用 write() 或 flush() 将导致抛出 IOException。
* 关闭以前关闭的流无效。
*/
fw.close();
// fw.write("eeeee");//异常IOException。
}
}
运行结果如下图所示:

08)IO异常的处理方式。
/*
* try{
* }catch(){
* }finally{
* }
*/
public class FileWriterDemo2 {
public static void main(String[] args) {
method();
}
public static void method(){
FileWriter fw = null;//在外部建立引用。
try{
fw = new FileWriter("WriterDemo2.txt");//在内部进行初始化。
fw.write("WriterDemo2.txt");
}catch(IOException e){
System.out.println("catch:" + e.toString());
}finally{
try{
if (fw != null)//如果文件不等于与NULL。
fw.close();//执行关闭流操作。
}catch(IOException e){
System.out.println(e.toString());//打印异常信息。
}
}
}
}
运行结果如下图所示:

09)文件的续写
/*
* FileWriter(String fileName, boolean append):
* 根据给定的文件名以及指示是否附加写入数据的 boolean 值来构造 FileWriter 对象。
*
* 显示对已有文件的数据的续写。
*/
public class FileWriterDemo3 {
public static void main(String[] args) {
method();
}
public static void method(){
FileWriter fw = null;
try{
//传递一个(true)参数,代表不覆盖已有文件。并在已有文件的末尾处进行续写。
fw = new FileWriter("WriterDemo.txt", true);
fw.write("\r\n换行续写!");
}catch(IOException e){
System.out.println("catch:" + e.toString());
}finally{
try{
if (fw != null)
fw.close();
}catch(IOException e){
System.out.println(e.toString());
}
}
}
}
运行结果如下图所示:

10)文本文件读取方式(一)
/*
* 第一种方式:调用读取流对象的read方法。
* int read():读取单个字符。
* 在字符可用、发生 I/O 错误或者已到达流的末尾前,此方法一直阻塞。
* 作为整数读取的字符,范围在 0 到 65535 之间 (0x00-0xffff),如果已到达流的末尾,则返回 -1
*/
public class FileReaderDemo {
public static void sop(Object obj){
System.out.print(obj + " ");
}
public static void main(String[] args) {
method();
}
public static void method(){
FileReader fr = null;
try{
//创建一个文件读取流对象,和指定名称的文件相关联。
//要保证文件是已经存在的,如果不存在,会发生异常:FileNotFoundException。
fr = new FileReader("WriterDemo2.txt");
int ch = 0;//设置读取下标从0开始。
while((ch = fr.read()) != -1)//只要有字符,就读取。
sop((char)ch);//read():一次读一个字符,会自动往下读。
}catch(IOException e){
System.out.println("catch:" + e.toString());
}finally{
try{
if (fr != null)
fr.close();
}catch(IOException e){
System.out.println("文件不存在异常:" + e.getMessage());
}
}
}
}
11)文本文件读取方式(二)
/*
* 第二种方式:通过字符数组进行读取。
*/
public class FileReaderDemo2 {
public static void sop(Object obj){
System.out.println(obj);
}
public static void main(String[] args) throws IOException{
method();
}
public static void method(){
FileReader fr = null;
try{
//创建一个文件读取流对象,和指定名称的文件相关联。
//要保证文件是已经存在的,如果不存在,会发生异常:FileNotFoundException。
fr = new FileReader("WriterDemo2.txt");
char[] buf = new char[1024];//常见定义长度。
int len = 0;
while((len = fr.read(buf)) != -1)
sop("第二种方式:通过字符数组进行读取。\n" + new String(buf, 0, len));
}catch(IOException e){
System.out.println("catch:" + e.toString());
}finally{
try{
if (fr != null)
fr.close();
}catch(IOException e){
System.out.println("文件不存在异常:" + e.getMessage());
}
}
}
}
运行结果如下图所示:

12)练习
/*
* 读取一个.java文件,并打印在控制台上。
*/
public class FileReaderTest {
public static void sop(Object obj){
System.out.print(obj);
}
public static void main(String[] args) {
method();
}
public static void method(){
FileReader fr = null;
try{
fr = new FileReader("RuntimeDemo.java");
char[] buf = new char[1024];
int len = 0;
while((len = fr.read(buf)) != -1)
sop(new String(buf, 0, len));
}catch(IOException e){
throw new RuntimeException("读取失败");
}finally{
if (fr != null){
try{
fr.close();
}catch(IOException e){
System.out.println("文件不存在异常:" + e.getMessage());
}
}
}
}
}
运行结果如下图所示(这是我RuntimeDemo.java文件中的内容):

13)拷贝文本文件
/*
*
* 复制的原理:
* 其实就是将C盘下的文件数据存储到E盘的一个文件中。
*
* 步骤:
* 1:在E盘(目标)创建一个文件。用于存储C盘(源文件)文件中的数据。
* 2:定义读(FileReader)取(FileWriter)流和C盘(源文件)文件关联。
* 3:通过不断的读(read)写(write)完成数据存储。
* 4:关闭(close)资源。
*/
public class CopyTestDemo {
public static void main(String[] args) {
method();
}
public static void method(){
FileWriter fw = null;
FileReader fr = null;
try{
fw = new FileWriter("E:\\RuntimeDemo_copy.txt");
fr = new FileReader("RuntimeDemo.java");
char[] ch = new char[1024];
for (int len = 0;(len = fr.read(ch)) != -1;)
fw.write(ch, 0, len);
}catch(IOException e){
throw new RuntimeException("读写失败");
}finally{
if (fr != null){
try{
fr.close();
}catch(IOException e){
}
}
if (fw != null){
try{
fw.close();
}catch(IOException e){
}
}
}
}
}
附言:我是Java新人,如有错误的地方请指出。
每天学习一点点,纠错一点点,进步很大点。