java中流与文件的心得体会_第 3 章 流与 Java 中的文件操作

3.1 , ?javajava.io.*,I/O

3.1 System System.out:()System.in() System.errmain,

3.1 ,?:System.out.println(bArray)::System.out.println(bArray);:String s=new String(bArray,0); System.out.println(s);System.inBufferedInputStreamSystem.outPrintStreamSystem.errPrintStream

3.2 JavaInputStreamOutputStream,InputStream:read():skip():available():mark():reset():markSupport():close():

3.2 read().int read():int read(byte b[]):int read(byte,int off,int len);OutputStream:write(int b):write(byte b[]):write(byte b[], int off,int len):bofflen

3.2 flush():close():.PrintStreamprintln()OutputStream,PrintStream,PrintStream:write, flush, checkError,print, println,close.println.: println(String s), println(char c)

3.3 File I/OFile fp=new File(tempfile.txt);FileInputStreamFileOutputStream

3.3 File I/OFileInputStream in=new FileInputStream(fp);FileOutputStream out=new FileOutputStream(fp);:()

3.3 File I/O,,file1.txtfile2.txt

3.3 File I/O:BufferedInputStream BufferedOutputStreamFileInputStream in=new FileInputStream(file1.txt);BufferedInputStream bin= new BufferedInputStream(in,256) int len; byte bArray[]=new byte[256];len=bin.read(bArray);len, bArray

3.3 File I/O,.Java,,.,flush().

3.4 ?.DataInputStreamDataOutputStream,,int,float,boolean.readLine: write,writeBoolean,read,readByte

3.4 FileOutputStream fos= new FileOutputStream(file2.txt"));DataInputStream dis= new DataInputStream(fos)

,,.

3.4 DateLine(InputStream in)(){ DataInputStream data=new DataInputStream(in); String currentLine; int lineCount=0; int charCount=0; while((currentLine=dataIn.readLine())!=null) { ++lineCount; charCount+=currentLine.length(); } return (charCount/(float)lineCount);}

3.5 RandomAccessFile zip,.new RandomAccessFile(file1.txt, r);new RandomAccessFile(file2.txt, rw);.

3.5 :readXXX()writeXXX()skipBytes();seek():getFilePointer():length():seek(long pos):32 temp.dat,seek,

3.6 fFile f=new File(data,temp.dat);f.getName(): temp.datf.getParent(): dataf.getPath(): data\temp.datf.getAbsolutePath(): c:\myprog\data\temp.dat

3.6 : : import java.io.*;public class file_size{ public static void main(String args[]) { File files=new File(.); String file_list[]=files.list(); for(int i=0;i

javaunicode DatainputStreamreadLine, , unicode

DataInputStreamunicodeInputStreamReader BufferedReader (jdk1.1)

3.7 java unicode

3.7 java unicodeimport java.io;()public class CharInput{ public static void main(String args[]) {String s; throws IOException InputStreamReader ir; BufferedReader in; ir=new InputStreamReader(System.in); in=new BufferedReader(ir); String s=in.readLine(); int i=Integer.parseInt(s); i=i*2; System.out.println(the result is +i); }}123

3.7 java unicode:,,

:ir=new InputStreamReader(is,8859_1);iso8859_1,.

*3.7 java unicodeimport java.io.*;class filetounicode{ public static void main(String args[]) { try{ FileInputStream fis=new FileInputStream("toyamei.txt"); InputStreamReader dis=new InputStreamReader(fis); BufferedReader reader=new String s; BufferedReader(dis); while((s=reader.readLine())!=null) { System.out.println("read: "+s);} dis.close(); }catch(IOException e) { } }}

3.8 PipedInputStreamPipedOutputStream:PipedInputStream pis=new PipedInputStream();PipedOutputStream pos=new PipedOutputStream(pis);:PipedOutputStream pos=new PipedOutputStream();PipedInputStream pis=new PipedInputStream(pos);

3.8 :,import java.io.*; class pipedstream { public static void main(String args[]) throws IOException{ byte aByteData1=123, aByteData2=111; PipedInputStream pis= new PipedInputStream(); PipedOutputStream pos= new PipedOutputStream(pis); System.out.println("PipedInputStream");

try{ pos.write(aByteData);pos.write(aByteData2);System.out.println((byte)pis.read());System.out.println((byte)pis.read());} finally { pis.close(); pos.close();}

3.8

3.9 java.io?,?.?.?.

3.9 :import java.net;import java.iopublic class GetString{ public String getStringFromUrl(URL inURL) { InputStream in; try { in =inURL.openStream(); }catch(IOException ioe) {System.out.printlin(URL error;+ioe); return null; } return getString(in); }url

3.9 public String getStringFromSocket(Socket inSocket){ inputStream in; try{ in=inSocket.getInputStreamSream(); }catch(IOEception ioe) {System.out.println(Socket error:+ioe); return null; } return getString(in);} socket

3.9 public String getString(inputStream inStream){ String readString = new String(); DataInputStream in =new DataInputSream(inStream); char inChar; try{ while(true) { inChar=in.readByte(); readString=readString+inChar; } }catch(EOFException eof) { System.out.println(readString);} }catch(IOException ioe) { { System.out.println(error:+ieo);} return readString; }

3.9 ?Class testObject{ int x; int y; float angle; String name; public testObject(int x,int y,float angle, String name); { this.x=x;this.y;this.angle;this.name=name;} }

3.9 ?1.serializablepublic class Student implements Serializable{ int id;String name; int age; String department; public Student(int id, String name,int age, String department) { this.id=id; this.name=name; this.age=age; this.department =departmernt; }}

3.9 2. (,):ObjectInput: ObjectOutputStream:writeObject():ObjectInputStream:readObject():jdk1.1

3.9 :Studentimport java.io.FileOutputStream;import java.io.ObjectOutputStream;public class Objectser{public static void main(String args[]) Student stu=new Student(981036,Li Ming,16,CSD);{try {FileOutputStream fo=new FileOutputStream(date.ser); ObjectOutputStream so=new ObjectOutputStream(fo); os.writeObject(stu);so.close(); }catch(Exception e) {;} }

3.9 import java.io.FileInputStream;import java.io.ObjectInputStream;public class ObjectRecov{ public static void main(String args[]) { Student stu; try {FileInputStream fi=new FileInputStream(date.ser); ObjectInputStream si=new ObjectInputStream(fi); stu=(Student)si.readObject();si.close(); }catch(Exception e) {System.out.println(e);} System.out.println(ID: +stu.id+name:+ stu.name+age:+age+dept.:+stu.department); }}

3.10 InputStreamOutputStream,.System.inSystem.out

3.10 InputStreamOutputStream,.System.inSystem.out

3.10 InputStream: read, available, mark, skip, reset, markSuppposed, closeOutputStream: write, flush, close

3.11 JavaI/O (,,)InputStream OutputStreamI/O(RandomAccessFile),readwrite,.,,.

3.11 File, File(Input/Output)Stream, RandomAccessFileData(Input/Output)Stream,, .: readByte, writeBoolean.Buffered(Input/Output)Stream,,.Piped(Input/Output)Stream

LOGO

******************************************

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值