流基本知识

流基本知识

各种流介绍

字节流:数据在存储时与传输时都是以字节为单位进行的。通常用于读写二进制数据,如图像和声音文件。

字符流:数据在存储与传输时都是以字符为单位进行的。

流:对数据源的一种抽象,其目的是想用统一的方式访问各种不同的数据源(文件、网络、内存的缓冲区)

文件读写的基本类:File类提供定位本地文件系统,描述文件和目录的功能。

管道流用于在线程之间通信:PipedInputStreamPipedOutputStreamPipedReaderPipedWriter

线程1àPipedOutputStreamàPipedInputStreamà线程2

键盘输入

try{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String s=br.readLine();

System.out.println(s);

}catch(Exception e){

}

文件输入输出

try{

File f=new File("test.txt");

FileOutputStream fout=new FileOutputStream(f);

fout.write(System.in.read());

fout.close();

FileInputStream fin=new FileInputStream(f);

int size=fin.available();

for(int i=0;i<size;i++){

System.out.print((char)fin.read());

}

fin.close();

}catch(Exception e){

}

 

通过网络传输文件

客户端:

File f=new File("bk.exe");

FileInputStream fin=new FileInputStream(f);

Socket st=new Socket("localhost",6000);

BufferedOutputStream bout=new BufferedOutputStream(st.getOutputStream());

int size=fin.available();

for(int i=0;i<size;i++){

bout.write(fin.read());

}

服务器端:

ServerSocket server=new ServerSocket(6000);

Socket st=server.accept();

File f=new File("kk.exe");

BufferedInputStream bin=new BufferedInputStream(st.getInputStream());

FileOutputStream fout=new FileOutputStream(f);

int i=0;

while(i!=-1){

i=bin.read();

fout.write(i);

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值