JavaIO输入输出学习之二

                     一、管道流  PipedOutputstream和PipedInputstream

1、管道流,作用是进行2个线程间的通讯
PipedOutputstream和PipedInputstream
2、PipedOutputstream中的方法:
connect(PipedInputStream snk)
          将此传送输出流连接到接收者。

3、编写一个管道流程序:
import java.io.* ;
class Send implements Runnable{   // 线程类
 private PipedOutputStream pos = null ; // 管道输出流
 public Send(){
  this.pos = new PipedOutputStream() ; // 实例化输出流
 }
 public void run(){
  String str = "Hello World!!!" ; // 要输出的内容
  try{
   this.pos.write(str.getBytes()) ;
  }catch(IOException e){
   e.printStackTrace() ;
  }
  try{
   this.pos.close() ;
  }catch(IOException e){
   e.printStackTrace() ;
  }
 }
 public PipedOutputStream getPos(){ // 得到此线程的管道输出流
  return this.pos ; 
 }
};
class Receive implements Runnable{
 private PipedInputStream pis = null ; // 管道输入流
 public Receive(){
  this.pis = new PipedInputStream() ; // 实例化输入流
 }
 public void run(){
  byte b[] = new byte[1024] ; // 接收内容
  int len = 0 ;
  try{
   len = this.pis.read(b) ; // 读取内容
  }catch(IOException e){
   e.printStackTrace() ;
  }
  try{
   this.pis.close() ; // 关闭
  }catch(IOException e){
   e.printStackTrace() ;
  }
  System.out.println("接收的内容为:" + new String(b,0,len)) ;
 }
 public PipedInputStream getPis(){
  return this.pis ;
 }
};
public class PipedDemo{
 public static void main(String args[]){
  Send s = new Send() ;
  Receive r = new Receive() ;
  try{
   s.getPos().connect(r.getPis()) ; // 连接管道
  }catch(IOException e){
   e.printStackTrace() ;
  }
  new Thread(s).start() ; // 启动线程
  new Thread(r).start() ; // 启动线程
 }
};

--------------------------------

 

打印流

 1.字节打印流PrintStream和字符打印流PrintWriter
  2、 用PrintStream类来操作文件的输出:

public class PrintDemo{
 public static void main(String arg[]) throws Exception{
  PrintStream ps = null ;  // 声明打印流对象
  // 如果现在是使用FileOuputStream实例化,意味着所有的输出是向文件之中
  ps = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "test.txt"))) ;
  ps.print("hello world") ;
  ps.close() ;
 }
};

2、格式化输出:
printf()

3、System类对Io包的支持
static PrintStream err
          “标准”错误输出流。
static InputStream in
          “标准”输入流。
static PrintStream out
          “标准”输出流。
OutputStream output=System.out;//从显示器输出
InputStream inut=System.in;//从键盘输入
在接受输入时开辟指定空间造成:1.空间太小,2、如果开辟的是奇数个空间。可能造成中文乱码。
解决1可以使用while循环。StringBuffer类。边读边写
解决2.需要开辟缓存区域,然后一次性取走。要用到BufferedReader类

 

4、.输入输出重定向。
System.out重定向。

public class SystemDemo{
 public static void main(String args[]) throws Exception {
  System.setOut(
   new PrintStream(
    new FileOutputStream("d:" +
     File.separator + "red.txt"))) ; // System.out输出重定向
  System.out.print("Helloword") ; // 输出时,不再向屏幕上输出
 }
};
通过重定向可以完成System.err的输出位置。

 

5、BufferedReader类
如果想接受任意长度的数据并避免乱码的产生,则可以使用bufferedreader类
构造方法:
BufferedReader(Reader in)
          创建一个使用默认大小输入缓冲区的缓冲字符输入流。但是System.in本身表示的是inputstream字节流,二要求接受的是个字符流,需要将字节流转化成字符流才可以。可以使用字节-字符转化流InputStreaReader来完成转换。

String readLine()
          读取一个文本行。

public class BufferedReaderDemo{
 public static void main(String args[]){
  BufferedReader buf = null ;  // 声明对象
  buf = new BufferedReader(new InputStreamReader(System.in)) ; // 将字节流变为字符流
  String str = null ; // 接收输入内容
  System.out.print("请输入内容:") ;
  try{
   str = buf.readLine() ; // 读取一行数据
  }catch(IOException e){
   e.printStackTrace() ; // 输出信息
  }
  System.out.println("输入的内容为:" + str) ;
 }
};

 

6、Scanner类
专门提供了输入数据类。此类可以完成BufferedReader类的功能。
此类存在import java.util包中;


public class ScannerDemo{
 public static void main(String args[]){
  Scanner scan = new Scanner(System.in) ; // 从键盘接收数据
  System.out.print("输入数据:") ;
  String str = scan.next() ; // 接收数据
  System.out.println("输入的数据为:" + str) ;
 }
};
比直接使用bufferedReader类方便。但是若键盘输入中带有空格则会只输出空格前面的内容。默认分隔符为空格。可以进行修改分隔符 Scanner useDelimiter(String pattern)
          将此扫描器的分隔模式设置为从指定 String 构造的模式。
Scanner类也可以方便的直接返回整数和小数;
1>。接受整数:先判断是否为整数 , boolean hasNextInt() ;
      int nextInt()
          将输入信息的下一个标记扫描为一个 int。

 

7、使用Scanner直接读取文件内容:
 
public class ScannerDemo{
 public static void main(String args[]){
  File f = new File("D:" + File.separator + "test.txt") ; // 指定操作文件
  Scanner scan = null ;
  try{
   scan = new Scanner(f) ; // 从键盘接收数据
  }catch(Exception e){}
  StringBuffer str = new StringBuffer() ;
  while(scan.hasNext()){
   str.append(scan.next()).append('/n') ; // 取数据
  }
  System.out.println("文件内容为:" + str) ;
 }
};

 

8、数据操作流 包装类
DataInputStream
DataOutputStream
DataOutputStream(OutputStream out)
          创建一个新的数据输出流,将数据写入指定基础输出流。

 

9、.合并流;
SequenceInputStream


public class SequenceDemo{
 public static void main(String args[]) throws Exception { // 所有异常抛出
  InputStream is1 = null ;  // 输入流1
  InputStream is2 = null ;  // 输入流1
  OutputStream os = null ;  // 输出流
  SequenceInputStream sis = null ; // 合并流
  is1 = new FileInputStream("d:" + File.separator + "a.txt") ;
  is2 = new FileInputStream("d:" + File.separator + "b.txt") ;
  os = new FileOutputStream("d:" + File.separator + "ab.txt") ;
  sis = new SequenceInputStream(is1,is2) ; // 实例化合并流
  int temp = 0 ; // 接收内容
  while((temp=sis.read())!=-1){ // 循环输出
   os.write(temp) ; // 保存内容
  }
  sis.close() ; // 关闭合并流
  is1.close() ; // 关闭输入流1`
  is2.close() ; // 关闭输入流2
  os.close() ; // 关闭输出流
 }
};

 

--------------------------------------------

1、字符编码
字符编码
1.计算机中只有数字。计算机一切都是用数字表示。。例如。字符a对应数字为97.此就是ACcII码。最高位bit为0。。一个字节
中文。gbk,gb2312;
2.Unicode编码
占2个字节

3.UTF-8
4.UTF-16

常用编码:ISO8859-1、GBK/GB2312 unicode UTF
System类中的System.getProperty("file.encoding");

 

2、对象序列化。
一个对象产生后实际上是在内存中为其开辟一个存储空间,方便存储信息。
对象序列化,就是把一个对象变为二进制的数据流的一种方法,通过对象序列化可以方便的实现对象的传输和存储。
如果一个类的对象想被序列化,则对象所有的类必须实现Java.io.Serializable接口。此接口中无方法,是标示接口
objectinputStream类和objectoutputStream类
对象中的transient和static类型的成员不会被读取和写入。

 

 

------------------------------------------

Io学习基本告一段落

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值