欢迎来到天天文库
浏览记录
ID:33458430
大小:71.30 KB
页数:3页
时间:2019-02-26
《javaio包内常用类及方法》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、java.io.*包内常用类及方法杜伟健版权声明:本资料只免费提供给2011级软件7班同学使用,其他任何未经授权的人擅自传播、印制,均视为侵权行为。一、文本文件(TextFile)1、输出(output)类(tothefile)(1)PrintWriter注意:不可以接文件名,可通过套用FileOutputStream来实现。例如PrintWriteroutputStream=newPrintWriter(newFileOutputStream("out.txt");常用方法:print()、p
2、rintln():写文件,close():保存并关闭文件。(2)FileOutputStream注意:可以接文件名。例:publicstaticvoidmain(String[]args){PrintWriteroutputStream=null;try{outputStream=newPrintWriter(newFileOutputStream("out.txt"));}catch(FileNotFoundExceptione){System.out.println("Erroropenin
3、gthefileout.txt.");System.exit(0);}System.out.println("Enterthreelinesoftext:");Stringline=null;intcount;for(count=1;count<=3;count++){line=scan.nextLine();outputStream.println(count+""+line);}outputStream.close();System.out.println("...writtentoout.
4、txt.");}2、输入(input)类(fromthefile)(1)BufferedReader注意:不可以接文件名,可通过套用FileReader来实现。例如:BufferedReaderinputStream=newBufferedReader(newFileReader("data.txt"));常用方法:read():读取一个字符,readLine():读取一行。读到文章结尾,返回-1。例:intcount=0;Stringline=inputStream.readLine();wh
5、ile(line!=null){count++;outputStream.println(count+""+line);line=inputStream.readLine();}补充:读取一个单词用java.util.*包内的StringTokenizer类例:StringinputLine=scan.nextLine();StringTokenizerwordFinder=newStringTokenizer(inputLine,".,");while(wordFinder.hasMore
6、Tokens()){System.out.println(wordFinder.nextToken());}(2)FileReader注意:可以接文件名。二、二进制文件(BinaryFile)1、输出(output)类(tothefile)(1)ObjectOutputStream注意:不可以接文件名,可通过套用FileReader来实现。例如:ObjectOutputStreamoutputStream=newObjectOutputStream(newFileOutputStream("nu
7、mbers.dat"));常用方法:writeInt(intn),writeDouble(doublex),writeBoolean(booleanb),writeUTF(Strings),writeChar((int)'A')写入数值,close():保存并关闭文件。(2)FileOutputStream注意:可以接文件名。2、输入(input)类(fromthefile)(1)ObjectInputStream注意:不可以接文件名,可通过套用FileReader来实现。例如:ObjectIn
8、putStreaminStream=newObjectInputStream(newFileInputStream("numbers.dat"));常用方法:readInt(),readDouble(),readBoolean()读取数值,close():保存并关闭文件。(2)FileInputStream注意:可以接文件名。例:publicstaticvoidmain(String[]args){StringfileName=null;try{System.out.println("Enter
此文档下载收益归作者所有