当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ObjectInputStream readLine()用法及代码示例


ObjectInputStream 类的 readLine() 方法读取以 \n、\r、\r\n 或 EOF 结尾的行。

用法

public String readLine()throws IOException

参数

没有传递参数。

返回

该行的字符串副本。

抛出

IOException - 如果发生其他 I/O 错误。

例子1

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ObjectInputStreamreadLineExample1  {
   public static void main(String[] args) throws IOException {
      
      String[] buf = {"JavaTpoint" , "  ","provide" , "  ","Tutorial" ,"  ", "for"  ,"  ","all" ," ", "language", "\n" };
      
      try {
         // create file output stream
       FileOutputStream   fileoutstream = new FileOutputStream("file1.txt");
         
         // create data output stream
        DataOutputStream dataoutstream = new DataOutputStream(fileoutstream);
         
         // for each byte in the buffer
         for (String b:buf) {
            // write character to the dos
            dataoutstream.writeChars(b);
         }
         
         // force bytes to the underlying stream
         dataoutstream.flush();
         
         // create file input stream
         InputStream inpstream = new FileInputStream("file1.txt");
         
         // create new data input stream
        DataInputStream datainpstream = new DataInputStream(inpstream);
         
         // read till end of the stream
       
         
            // read character
            String c = datainpstream.readLine();
            
            // print
            System.out.print(c);
         
         
      } catch(Exception e) {
            
         e.printStackTrace();
      } 
   }
}

输出:

JavaTpoint provide tutorial for all language

例子2

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ObjectInputStreamreadLineExample2 {
   public static void main(String[] args) throws IOException {
      
      String[] carray = {"JavaTpoint" , "  ","provide" , "  ","Tutorial" ,"  ", "for"  ,"  ","all" ," ", "language" ,"\n"};
     
      
      try {
         // create file output stream
       FileOutputStream   fileoutstream = new FileOutputStream("barray");
         
         // create data output stream
        DataOutputStream dataoutstream = new DataOutputStream(fileoutstream);
         
         // for each byte in the buffer
         for (String b:carray) {
            // write character to the dos
            dataoutstream.writeChars(b);
         }
         
         // force bytes to the underlying stream
         dataoutstream.flush();
         
         // create file input stream
         InputStream inpstream = new FileInputStream("barray");
         
         // create new data input stream
        DataInputStream datainpstream = new DataInputStream(inpstream);
         
         // read till end of the stream
         for(int i=0;i<=carray.length;i++){ 
         
            // read double values in float format
            String c = datainpstream.readLine();
            
            // print
            System.out.print("    "+c);
         }
         
      } catch(Exception e) {
        
      } 
   }
}

输出:

JavaTpoint provide tutorial for all language




相关用法


注:本文由纯净天空筛选整理自 Java ObjectInputStream readLine() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。