付费下载
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第二十讲Java输入输出流基本要求1理解Java中流的概念掌握字节流、字符流的使用掌握File类的使用2一、文件类File文件可以分为文本文件、随机文件和二进制文件,都可通过File类来进行简单操作。特点:灵活,功能多创建文件流:常用文件名或File类的对象创建文件流。文件过滤:将符合条件的文件选择出来进行操作,通过接口FileFilter和FilenameFilter来实现。、创建临时文件、改变文件名、删除提供对文件进行创建文件等操作提供获取文件信息的方法,如文件名、文件路径和文件长度等File类的构造方法:public
File(String
pathname)public
File(String
parent,String
child)public
File(File
parent,String
child)public
File(URI
uri)(1)File类的方法1、文件对象public
String
getName(
)public
String
getPath(
)public
String
getAbsolutePath(
)public
String
getParent()public
File
getParentFile(
)2、文件操作public
boolean
renameTo(File
dest)public
boolean
delete()3、获得文件的属性public
long
length()public
boolean
exists()public
long
lastMoidfied()4、操作public
boolean
mkdir()public
String[
]
list()public
File[
]
listFiles();34例子:File类中常用方法使用publicclassFileExample{public
static
voidmain(String[]
agrs){Filefile
=newFile("F:\\Java\\10\\FileExample.java");System.out.println("文件或
是否存在:"
+
file.exists());System.out.println("是文件吗:"
+
file.isFile());System.out.println("是
吗:"
+
file.isDirectory());System.out.println("名称:"
+ file
.getName());System.out.println("路径:
"
+
file.getPath());System.out.println("绝对路径:
"
+
file.getAbsolutePath());System.out.println("最后修改时间:"
+
file.lastModified());System.out.println("文件大小:"
+
file.length()+
"
字节");}}5例:File类中常用方法使用import
java.io.File;import
java.util.Date;public
class
File_ex{void
FileInformation(Filef){System.out.println(f.getName());System.out.println(f.getAbsolutePath());System.out.println(f.getParent());System.out.println(f.length());System.out.println(newDate(f.lastModified()));}void
DirectoryInformation(Filed){System.out.println(d.getName());System.out.println(d.getParent());int
i=0;String
lt[]=d.list();while(i<lt.length){System.out.println(lt[i]);i++;}}public
static
void
main(String
args[]){//File
f=new
File("D:/java/","file1.text");File
d=new
File("D:/");//File
d1=new
File("D:/java/");File_ex
fe=new
File_ex();//fe.FileInformation(f);fe.DirectoryInformation(d);//
d1.mkdir();}}二、流的概念java程序通过流来完成输入/输出,流是程序中各种信息
的抽象。可以处理文件、内存、网络数据等多种数据形式。数据流是从源到目的的字节的有序序列,先进先出,像管道一样。两种基本流:InputStream(输入流)和OutputStream(输出流)6(1)流式输入输出的特点每个数据都必须等待排在它前面的数据读入或送出之后才能被读写每次读写操作处理的都是序列中剩余的未读写数据中的第一个,而不能随意选择输入输出的位置Java中流的实现是在java.io包定义的类层次结构完成的。所以要在程序中使用流类,必须java.io包78(2)
Java的标准输入输出标准输入输出是指在命令行方式下的输入输出方式。Java通过System.in、System.out和System.err来实现标准输入输出和标准错误输出。每当main方法被执行时,就自动生成System.in、System.out和System.err三个对象。System.in是字节输入流InputStream类的一个对象,其中有
read方法从键盘读入数据:public
int
read()
throws
IOExceptionpublic
int
read(byte[
]
b)
throws
IOExceptionSystem.out是流PrintStream类的一个对象,其中print和println方法向屏幕输出数据。System.err是流PrintStream类的一个对象,用于向屏幕输出错误信息9例子:注意异常处理public
class
Demo
{public
static
void
main(String
args[]){byte
buffer[
]=new
byte[200];int
i,d=0,count=0;System.out.print("Input
a
string:
");try{count=System.in.read(buffer);}catch(Exception
e){System.err.println("发生异常:"+e.toString());}for(i=0;i<=count-1;i++)System.out.print((char)buffer[i]);System.out.println(count);System.out.println("Input ten
char:
");for(i=1;i<=10;i++)try{d=System.in.read();System.out.println((char)d);}catch(Exception
e){System.err.println("发生异常:"+e.toString());}}}10(3)
Java的数据流Java的数据流都在java.io包里Java的数据流根据操作的数据流分为字节流和字符流字节流:流中的数据以8位字节为单位进行读写,以InputStream和OutputStream为基础类。字符流:流中的数据以16位字节为单位进行读写,以Reader和Writer为基础类。三、字节流InputStream和OutputStream分别是字节输入流和字节输出流的超类,查看图。InputStream和OutputStream提供许多用于字节输入输出的方法,包括:数据的数据的写入标记位置获取数据量关闭数据流11(1)字节输入流InputStream类的层次结构12三个基本read()方法int
read() //读一个字节返回int
read(byte[]) //将数据读入byte[],返回读的字节数int
read(byte[],
int
offset,
int
length
)其它方法void
close() //关闭流。自顶向下关闭Filter
streamint
available()//返回未读的字节数long
skip(longn) //跳过n个字节booleanmarkSupported()//测试打开的流是否支持书签void
mark(int) //标记当前流,并建立int大小缓冲区void
reset(
) //
返回
出InputStream
常用方法13例子:字节文件输入流--FileInputStream//
文件内容并显示在屏幕public
class
Demo{public
static
void
main(String
args[]){try{FileInputStream
rf=newFileInputStream("D:\\mytext.txt");int
b;while((b=rf.read())!=-1)System.out.print((char)b);rf.close();}catch(IOException
ie){System.out.println(ie);}catch(Exceptione){System.out.println(e);}}}14字节输出流OutputStream类层次1516OutputStream方法三个基本的write()方法void
write(int)//写一个字节void
write(byte[])//写一个字节数组voidwrite(byte[],int
offset,
intlength
)其它方法void
close(
)void
flush()//强行写入例子:字节文件输出流----FileOutputStream//
文件publicclassDemo{public
static
voidmain(String
args[]){try{Filefile=newFile("D:/file2.txt");FileOutputStream
wf=newFileOutputStream(file,true);String
s="向文件中写入一个字符串";byteb[]=s.getBytes();wf.write(b);wf.close();}catch(Exception
e){e.printStackTrace();}}}17总结:FileInputStream和FileOutputStream实现了对文件的顺序,以字节为单位对文件进行读写操作,主要有这样几步:创建文件输入输出流的对象打开文件用文件读写方法读写数据关闭数据流18四、字符流类Reader是字符输入流的抽象超类,其提供的方法与InputStream类似,只是将基于Byte的参数改为基于Char。类Writer是字符输出流的抽象超类,其提供的方法与OutputStream类似,只是将基于Byte的参数改为基于Char。Reader和Writer
类实现字节和字符间的自动转换。每一个
输入、输出流,都有相应的Reader和Writer版本。19Reader的类层次结构20Reader的基本方法int
read();//读单个字符int
read(char
cbuf[]);//读字符放入数组中int
read(char
cbuf[],int
offset,
int
length);//读字符放入数组的指定位置void
close() //关闭流。long
skip(long
n) //跳过n个字符boolean
markSupported(
)
//测试打开的流是否支持书签void
mark(int) //标记当前流,并建立int大小缓冲区void
reset(
) //
返回
出boolean
ready() //测试当前流是否准备好进行读21Writer的类层次结构2223Writer的基本方法int
write(int
c);//写单个字符int
write(char
cbuf[]);//写字符数组int
write(char
cbuf[],
int
offset,
int
length)
;int
write(String
str)
;int
write(String
str,
int
offset,
int
length)
;void
close(
)void
flush()//强行写入24例:从键盘输入文字存入文件,再读出加上行号后打印在屏幕//字符缓冲流:BufferedReader和BufferedWriter,以缓冲区方式对数据进行输入输出。public
class
Demo{public
static
void
main(String
[]args){String
f="d:\\file2.txt";String
str="";int
i=0;try{BufferedReader
keyIn=new
BufferedReader(new
InputStreamReader(System.in));BufferedWriter
bw=new
BufferedWriter(new
FileWriter(f));BufferedReader
br
=
new
BufferedReader(new
FileReader(f));System.out.println("Please
input
file
text:");while(!(str=keyIn.readLine()).equals("exit")){bw.write(str,0,str.length());bw.newLine();}bw.close();while((str=br.readLine())!=null){i++;System.out.println(i+":
"+str);}}catch(IOException
e){
}}}总结:字节流与字符流的比较Reader
和InputStream以及Writer与
OutputStream定义的API类似,但操作的数据类型不同。所有的流——InputStream、OutputStream
、Reader、
Writer
在创建时自动打开;程序中可以调用close方法关闭流,否则Java运行环境的
收集器将隐含将流关闭。2526输入输出流类分类I/O
StreamsType
ofI/OStreamsDescriptionMemoryCharArrayReaderCharArrayWriterByteArrayInputStreamByteArrayOutputStream从/向内存数组读写数据.StringReaderStringWriterStringBufferInputStream从/向内存字符串读写数据PipePipedReaderPipedWriterPipedInputStreamPipedOutputStream实现管道的输入和输出FileFileReaderFileWriterFileInputStreamFileOutputStream统称为文件流。对文件进行读、写操作27I/O
StreamsTypeofI/OStreamsDescriptionObjectSerializationObjectInputStreamObjectOutputStream对象的输入、输出DataConversionDataInputStreamDataOutputStream读、写基本数据类型PrintingPrintWriterPrintStream包含方便的打印方法BufferingBufferedReaderBufferedWriterBufferedInputStreamBufferedOutputStream在读入或写出时,对数据进行缓存,以减少I/O的次数。I/O
StreamsType
ofI/OStreamsDescriptionFilteringFilterReaderFilterWriterFilterInputStreamFilterOutputStream过滤流,在数据进行读或写时进行过滤。ConcatenationSequenceInputStream把多个输入流连接成一个输入流CountingLineNumberReaderLineNumberInputStream在读入数据时对行记数Peeking
AheadPushbackReaderPushbackInputStream通过缓存机制,进行预读Converting
betweenBytes
and
CharactersInputStreamReaderOutputStreamWriter按照一定的编码/
标准将字节流转换为字符流,或进行反向转换。28五、RandomAccessFile类FileInputStream、FileOutputStream、FileReader和FileWriter流只能完成单一的功能,要么只能
,要么只能写入。Java还提供了专门用来处理文件输入输出功能更加完善的RandomAccessFile流当想对一个文件进行读写操作时,就可以创建一个指向该文件的RandomAccessFile流即可,这样既可以从这个流中获取文件信息,也可以向这个流中写入信息。29例子:参考P334例import
java.io.*;public
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年集团“安全生产月”测试题附答案
- 2026年检验检测机构从业人员安全教育培训试题及答案
- 2026年临床执业医师资格考试模拟试题及答案(儿科学)
- 2026年煤矿培训考核试题库150道及参考答案【轻巧夺冠】
- 2026年农机驾驶证考试题及答案
- 2026年期货从业法律法规试题(附答案)
- 2026年人工智能通识认证考核试题含答案
- 2026年全科医学科主治医师病例分析试题及标准答案集
- 第五单元 习作与例文:生活万花筒 课件 2026-2027学年统编版语文四年级上册
- 《张量分析及其在力学中的应用》教案全套 -程国强
- 再生障碍性贫血诊断与治疗中国指南
- 精神病人警情处置规范与实战
- 早读知识点(课件)-2025-2026学年二年级上册数学人教版
- 办公楼公区装修木工报价清单
- 监察法课件教学课件
- 惊恐障碍的认知行为干预与药物协同
- 声东击西课件
- 2025年服务机器人应用技术员职业技能竞赛题库(含答案)
- JCT 2966-2025混凝土用生活垃圾焚烧飞灰-知识培训
- 《拒绝不文明语言》主题班会课件
- 五年级数学小数乘法教学案例分享
评论
0/150
提交评论