黑马程序员————IO流1(day18)

----------------------ASP.Net+Android+IOS开发----------------------期待与您交流!

 

 

IO1

l  其它对象(System

l  其它对象(Runtime

l  其它对象(Date

l  其它对象(Calendar

l  其它对象(Math - Random

l  IO流(概述)

l  IO流(FileWriter

l  IO流(IO异常处理方式)

l  IO流(文件的续写)

l  IO流(文本文件读取方式一)

l  IO流(文本文件读取方式二)

l  IO流(拷贝文本文件)

 

 

其它对象(System

System:类中的方法和属性都是静态的。

out:标准输出,默认是控制台。

in标准输入,默认是键盘。

 

描述系统一些信息,获取系统属性信息:Properties getProperties();

 

示例:

import java.util.*;

class SystemDemo {
	public static void main(String[] args) {
		Properties prop = System.getProperties();

		// 因为Properties是Hashtable的子类,也就是Map集合的一个子类对象。
		// 那么可以通过map的方法取出该集合中的元素。
		// 该集合中存储都是字符串。没有泛型定义。

		// 如何在系统中自定义一些特有信息呢?
		System.setProperty("mykey", "myvalue");

		// 获取指定属性信息。
		String value = System.getProperty("os.name");
		System.out.println("value=" + value);

		// 可不可以在jvm启动时,动态加载一些属性信息呢?
		String v = System.getProperty("haha");
		System.out.println("v=" + v);
		// 必须在命令窗口里输入java -D名称=值 类名
		// 例如:java -Dhaha=xxx SystemDemo

		/*
		 * //获取所有属性信息。 for(Object obj : prop.keySet()) { String value =
		 * (String)prop.get(obj);
		 * 
		 * System.out.println(obj+"::"+value); }
		 */
	}
}


 

其它对象(Runtime

Runtime对象,该类并没有提供构造函数,说明不可以new对象。那么会直接想到该类中的方法都是静态的,发现该类中还有非静态方法,说明该类肯定会提供了方法获取本类对象。而且该方法是静态的,并返回值类型是本类类型。

由这个特点可以看出该类使用了单例设计模式完成,该方式是static Runtime getRuntime();

 

示例:

class RuntimeDemo {
	public static void main(String[] args) throws Exception {
		Runtime r = Runtime.getRuntime();//返回与当前Java应用程序相关的运行时对象
		Process p = r.exec("notepad.exe  SystemDemo.java");//打开名叫SystemDemo.java的记事本文

		// Thread.sleep(4000);
		// p.destroy();//杀掉进程,结束它
	}
}


 

其它对象(Date

示例:

import java.util.*;
import java.text.*;

class DateDemo {
	public static void main(String[] args) {
		Date d = new Date();
		System.out.println(d);// 打印的时间看不懂,希望有些格式。

		// 将模式封装到SimpleDateformat对象中。
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日E hh:mm:ss");

		// 调用format方法让模式格式化指定Date对象。
		String time = sdf.format(d);
		System.out.println("time=" + time);

		long l = System.currentTimeMillis();
		Date d1 = new Date(l);
		System.out.println("d1:" + d1);
	}
}


 

运行结果:Wed May 01 16:24:57 CST 2013

time=20130501日星期三 04:24:57

d1:Wed May 01 16:24:57 CST 2013

 

 

其它对象(Calendar

示例:

import java.util.*;

class CalendarDemo {
	public static void main(String[] args) {

		Calendar c = Calendar.getInstance();

		String[] mons = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月",
				"十月", "十一月", "十二月" };

		String[] weeks = { "", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", };
		int index = c.get(Calendar.MONTH);
		int index1 = c.get(Calendar.DAY_OF_WEEK);

		sop(c.get(Calendar.YEAR) + "年");
		// sop((c.get(Calendar.MONTH)+1)+"月");
		sop(mons[index]);
		sop(c.get(Calendar.DAY_OF_MONTH) + "日");
		// sop("星期"+c.get(Calendar.DAY_OF_WEEK));
		sop(weeks[index1]);

		/*
		 * Date d = new Date();
		 * 
		 * SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		 * 
		 * String year = sdf.format(d);
		 * 
		 * System.out.println(year);
		 */
	}

	public static void sop(Object obj) {
		System.out.println(obj);
	}
}


 

运行结果:2013

五月

1

星期三

 

 

其它对象(Math - Random

示例:

import java.util.*;

class MathDemo {
	public static void main(String[] args) {
		/*
		 * Random r = new Random(); for(int x=0; x<10; x++) { //int d =
		 * (int)(Math.random()*10+1); int d = r.nextInt(10)+1; sop(d); }
		 */
		saveTwo(12.3456, 3, true);// 12.34
	}

	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;

		sop("num=" + num);

		/*
		 * double d1 = d*100; sop("d1="+d1); d1 = d1+0.5; double d2 = (int)d1;
		 * sop("d2="+d2); double d3 = d2/100; sop("d3="+d3);
		 */
	}

	public static void show() {
		double d = Math.ceil(16.34);// ceil返回大于指定数据的最小整数。
		double d1 = Math.floor(12.34);// floor返回小于指定数据的最大整数。

		long l = Math.round(12.54);// 四舍五入
		sop("d=" + d);
		sop("d1=" + d1);
		sop("l=" + l);

		double d2 = Math.pow(2, 3);
		sop("d2=" + d2);
	}

	public static void sop(Object obj) {

		System.out.println(obj);
	}
}


 

运行结果:num=12.346

 

 

IO流(概述)

IO(Input Output)

大多数的应用程序都需要外部设备进行数据交换,最常见的外部设备包含磁盘和网络。IO就是指应用程序对这些设备的数据输入与输出。在程序中,键盘被用做文件输入,显示器被用做文件输出。JAVA语言定义了许多类专门负责各种方式的输入输出,这些类都被放在java.io包中。

流按操作数据分为两种:字节流和字符流。

流按流向分为:输入流,输出流。

 

IO流常用基类

Java的流式输入/输出建立在4个抽象类的基础上:InputStreamOutputStreamReaderWriter它们用来创建具体流式子类。尽管程序通过具体子类进行输入/输出操作,但顶层的类定义了所有流类的基本通用功能。

InputStreamOutputStream被设计成字节流类。

ReaderWriter被设计成字符流类。

字节流和字符流类行程分离的层次结构。一般来说,处理字符或字符串时应使用字符流类,处理字节或二进制对象时应使用字节流类。

一般在操作文件流时,不管是字节流还是字符流,都可以按照以下的方式进行:

1.  使用File类找到一个文件。

2.  通过File类的对象去实例化字节流或字符流的子类。

3.  进行字节(字符)的读、写操作。

4.  关闭流文件。

 

 

IO流(FileWriter

字符流:

字符流层次结构的顶层是ReaderWriter抽象类。

 

既然IO流是用于操作数据的,那么数据的最常见体现形式是:文件。那么先以操作文件为主来演示。

 

需求:在硬盘上,创建一个文件并写入一些文字数据。

 

找到一个专门用于操作文件的Writer子类对象。FileWriter 后缀名是父类名。前缀名是该流对象的功能。

 

示例:

import java.io.*;

class FileWriterDemo {
	public static void main(String[] args) throws IOException {
		// 创建一个FileWriter对象。该对象一被初始化就必须要明确被操作的文件。
		// 而且该文件会被创建到指定目录下。如果该目录下已有同名文件,将被覆盖。
		// 其实该步就是在明确数据要存放的目的地。
		FileWriter fw = new FileWriter("demo.txt");

		// 调用write方法,将字符串写入到流中。
		fw.write("abcde");

		// 刷新流对象中的缓冲中的数据。
		// 将数据刷到目的地中。
		fw.flush();

		// 关闭流资源,但是关闭之前会刷新一次内部的缓冲中的数据。
		// 将数据刷到目的地中。
		// 和flush区别:flush刷新后,流可以继续使用,close刷新后,会将流关闭。
		// fw.close();
	}
}


 

IO流(IO异常处理方式)

示例:

import java.io.*;

class FileWriterDemo2 {
	public static void main(String[] args) {
		FileWriter fw = null;
		try {
			fw = new FileWriter("demo.txt");
			fw.write("abcdefg");

		} catch (IOException e) {
			System.out.println("catch:" + e.toString());
		} finally {
			try {
				if (fw != null)
					fw.close();
			} catch (IOException e) {
				System.out.println(e.toString());
			}

		}
	}
}


 

IO流(文件的续写)

演示对已有文件的数据续写。

 

示例:

import java.io.FileWriter;
import java.io.IOException;

public class FileWriterDemo3 {
	public static void main(String[] args) {
		FileWriter fw = null;
		try {
			fw = new FileWriter("demo.txt", true);
			fw.write("abcdefg\rxiexie");// \r换行

		} catch (IOException e) {
			System.out.println("catch:" + e.toString());
		} finally {
			try {
				if (fw != null)
					fw.close();
			} catch (IOException e) {
				System.out.println(e.toString());
			}

		}
	}
}


 

IO流(文本文件读取方式一)

示例:

import java.io.*;

class FileReaderDemo {
	public static void main(String[] args) {
		// 创建一个文件读取流对象,和指定名称的文件相关联。
		// 要保证该文件是已经存在的,如果不存在,会发生异常FileNotFoundException
		FileReader fr = null;
		try {
			fr = new FileReader("demo.txt");
			// 调用读取流对象的read方法。
			// read():一次读一个字符。而且会自动往下读。
			int ch = 0;
			while ((ch = fr.read()) != -1) {
				System.out.println("ch=" + (char) ch);
			}
		} catch (IOException e) {
			System.out.println(e.toString());
		} finally {
			try {
				fr.close();
			} catch (IOException e) {
				System.out.println(e.toString());
			}
		}
	}
}


 

IO流(文本文件读取方式二)

通过字符数组进行读取。

 

示例:

import java.io.*;

class FileReaderDemo2 {
	public static void main(String[] args) {
		FileReader fr = null;
		try {
			fr = new FileReader("demo.txt");
			// 定义一个字符数组。用于存储读到字符。
			// 该read(char[])返回的是读到字符个数。
			char[] buf = new char[1024];
			int num = 0;
			while ((num = fr.read(buf)) != -1) {
				System.out.println(new String(buf, 0, num));
			}
		} catch (IOException e) {
			System.out.println(e.toString());
		} finally {
			try {
				fr.close();
			} catch (IOException e) {
				System.out.println(e.toString());
			}
		}
	}
}


 

读取一个.txt文件,并打印在控制台上。

 

示例:

import java.io.*;

class FileReaderTest {
	public static void main(String[] args) {
		FileReader fr = null;
		try {
			fr = new FileReader("demo.txt");
			char[] buf = new char[1024];
			int num = 0;
			while ((num = fr.read(buf)) != -1) {
				System.out.print(new String(buf, 0, num));
			}
		} catch (IOException e) {
			System.out.println(e.toString());
		} finally {
			try {
				fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}


 

IO流(拷贝文本文件)

示例:

import java.io.*;

class CopyText {
	public static void main(String[] args) throws IOException {
		copy_2();
	}

	public static void copy_2() {
		FileWriter fw = null;
		FileReader fr = null;
		try {
			fw = new FileWriter("IOSystemDemo_copy.txt");
			fr = new FileReader("SystemDemo.java");

			char[] buf = new char[1024];
			int len = 0;
			while ((len = fr.read(buf)) != -1) {
				fw.write(buf, 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) {
				}
		}
	}

	// 从C盘读一个字符,就往D盘写一个字符。
	public static void copy_1() {
		FileWriter fw = null;
		FileReader fr = null;
		try {
			// 创建目的地。
			fw = new FileWriter("RuntimeDemo_copy.txt");
			// 与已有文件关联。
			fr = new FileReader("RuntimeDemo.java");
			int ch = 0;
			while ((ch = fr.read()) != -1) {
				fw.write(ch);
			}
		} catch (IOException e) {
			System.out.println(e.toString());
		} finally {
			if (fr != null)
				try {
					fr.close();
				} catch (IOException e) {
				}
			if (fw != null)
				try {
					fw.close();
				} catch (IOException e) {
				}
		}
	}
}


 

 

 

----------------------ASP.Net+Android+IOS开发----------------------期待与您交流!

详情请查看:http://edu.csdn.net


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值