您的位置:首页 > 编程语言 > Java开发

Java笔记5 IO<1>其他对象以及IO流

2013-05-22 22:59 639 查看
------- android培训java培训、期待与您交流! ----------

18天-01-其他对象(System)
System:类中的属性和方法都是静态的。
out:标准输出流,默认是控制台。
in:标准输入,默认是键盘。

18天-03-其他对象(Date)
示例:
import java.text.SimpleDateFormat;
import java.util.Date;

public
class
DateDemo1 {
public
static void
main(String[] args) {
Date d = new Date();
System.out.println(d);

//将模式封装到SimpleDateFormat对象中。
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd
E HH时mm分ss秒SSS");
//调用farmat方法让模式格式化指定date对象。
String dateStr = format.format(d);
System.out.println(dateStr);
}
}

18天-04-其他对象(Calendar)
l 在应用Calendar方法时要注意以下几点:
1) 用静态方法获取Calendar对象,Calendar c = Calendar.getInstance();
2) 一周中的天数(1~7),一周中的第一天:星期日,依此类推,第七天是:星期六;
3) public final void set(int year,int month, int date),其中,参数month的值是基于0的。
c.set(2013,2,8);设置时间为:2013年3月8日

18天-05-其他对象(Math-Random)
import java.util.Random;

public
class
MathDemo {
static
void
main(String[] args) {
double d = Math.ceil(15.22);//结果:16.0。ceil返回大于指定数据的最小//整数。
double d1 = Math.floor(15.22);//结果:15.0。floor返回小于指定数据的//最大整数。
long l = Math.round(15.50);//结果:16。四舍五入。
doubled2 = Math.random();//结果:0.0<=d2<1.0

Random r = new Random();
int
a
= r.nextInt();//结果:0<=a<=9
System.out.println(l);
}
}

18天-06-IO流(概述)
l IO(Input Output)流
Ø 流按操作数据分两种:字节流和字符流。
Ø 流按流向分为:输入流和输出流。
l IO流常用基类
Ø 字节流的抽象基类:
Ÿ InputStream,OutputStream
Ø 字符流的抽象基类
Ÿ Reader,Writer
² 注:由这四个类派生出来的子类名称就是以其父类作为子类名的后缀。
Ø 如:InputStream的子类FileInputStream
Ø 如:Reader的子类FileReader。

18天-07-IO流(FileWriter)
举例:
import java.io.FileWriter;
import java.io.IOException;

public
class
FileWriterDemo {

public
static void
main(String[] args) {
FileWriter fw = null;
try {
//1.创建一个FileWriter对象。该对象已初始化就必须要明确被操作的文件。即明确数据要存放的目的地。
//注意:(1)该文件会被创建到指定的目录下,如果该目录已有同名文件,将被覆盖。
fw = new FileWriter("demo.txt");
//2.调用write方法,将字符串写到流中。
fw.write("abcde");

//3.刷新对象流中的缓冲数据,将数据刷新到目的地中。
//fw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(fw!=null){
try {
//4.关闭流资源(带刷新功能),但是关闭之前会刷新一次内部缓冲中的数据,将数据刷到目的地中。
//close和flush的区别:flush刷新后,流可以继续使用,close刷新后,会将流关闭。
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

18天-10-IO流(文本文件读取方式一)
l 第一种方式:通过字符进行读取。
import java.io.FileReader;
import java.io.IOException;

public
class
FileReaderDemo {

public
static void
main(String[] args)throws IOException {
//要保证文件存在,否则会抛异常
FileReader fr = new FileReader("Demo.txt");

int ch = 0;
while((ch=fr.read())!=-1){//达到流的末尾返回-1循环结束。
System.out.println((char)ch);
}
}
}

18天-11-IO流(文本文件读取方式二)
l 第二种方式:通过字符数组进行读取。
比第一种方式运行效率更高
import java.io.FileReader;
import java.io.IOException;

public
class
FileReaderDemo1 {

public
static void
main(String[] args)throws IOException {
//1.创建文件读取刘对象
FileReader fr = new FileReader("Demo.txt");

//2.定义缓冲区,读取缓冲区数据并打印。
char[] buf =newchar[1024];
int len = 0;//要读取的最多的字符数
while((len=fr.read(buf))!=-1){
System.out.println(new String(buf,0,len));
}
//4.关闭流资源
fr.close();
}
}

18天-13-IO流(拷贝文本文件)
l 将C盘一个文本文件复制到D盘。
² 复制原理:其实就是讲C盘下的文件数据存储到D盘的一个文件中。
步骤:
1. 在D盘创建一个文件,用于存储C盘文件中的数据。
2. 定义读取流和C盘文件关联。
3. 通过不断的读写完成数据存储。
4. 关闭流资源。
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public
class
CopyText {

public
static void
main(String[] args) {
copy();
}

public
static void
copy(){
//创建目的地
FileWriter fw = null;
//与已有文件关联。
FileReader fr = null;
try {
fw = new FileWriter("D:"+File.separator+"Demo.txt",true);
fr = new FileReader("C:"+File.separator+"Demo.txt");

int ch = 0;
while((ch=fr.read())!=-1){
fw.append((char)ch);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
//关闭流资源
if(fr!=null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fw!=null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

------- android培训java培训、期待与您交流! ----------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: