Java의 Java.io.LineNumberInputStream 클래스

Java의 Java.io.LineNumberInputStream 클래스

java.io.LineNumberInputStream 클래스는 단순히 현재 줄 번호의 기록을 유지하기 위한 추가 기능을 제공하는 입력 스트림의 확장입니다. 

'r'로 끝나는 바이트 시퀀스입니다. 즉, 캐리지 리턴 문자 또는 개행 문자: 'n' 또는 캐리지 리턴 문자 뒤의 줄 바꿈 문자입니다.

선언 :   

public class LineNumberInputStream extends Reader 

생성자:   

  LineNumberInputStream(InputStream in) :    Constructs a newline no. stream that reads it's input from the specified Input Stream. 

행동 양식:   

LineNumberInputStream 클래스

    읽기() : java.io.LineNumberInputStream.read() 입력 스트림에서 데이터의 다음 바이트를 읽습니다. '0 - 255' 범위의 바이트를 나타내는 int 값을 반환합니다. 입력 스트림의 끝을 나타내기 위해 '-1'을 반환합니다. 
    구문: 
public int read()   Parameters :    -------   Return :    int value representing the bytes in the range of '0 - 255'. return -1 indicating end of Input Stream.   Exception:    IOException : in case I/O error occurs 

구현 :  

Java
   // Java program illustrating the working of read() method   import     java.io.*  ;   public     class   NewClass   {      public     static     void     main  (  String  []     args  )     throws     IOException      {      // LineNumberInputStream & FileInputStream initially null      LineNumberInputStream     geekline     =     null  ;      FileInputStream     geekinput     =     null  ;      try  {      char     c  ;      int     a  ;      // New InputStream : 'ABC' is created      geekinput     =     new     FileInputStream  (  'ABC.txt'  );      geekline     =     new     LineNumberInputStream  (  geekinput  );      // read() method returning Bytes of Input Stream as integer      // '-1' indicating to read till end Of Input Stream      while  ((  a     =     geekline  .  read  ())     !=     -  1  )      {      // Since read() method returns Integer value      // So we convert each integer value to char      c     =     (  char  )  a  ;      System  .  out  .  print  (  c  );      }      }      catch  (  Exception     e  )      {      // In case of error      e  .  printStackTrace  ();      System  .  out  .  println  (  'ERROR Occurs '  );      }      finally      {      // Closing the streams Once the End of Input Stream is reached      if  (  geekinput     !=     null  )      geekinput  .  close  ();      if  (  geekline     !=     null  )      geekline  .  close  ();      }      }   }   

메모 : 
온라인 IDE의 파일에 액세스할 수 없으므로 다음 Java 코드는 여기서 실행되지 않습니다. 
따라서 프로그램을 시스템에 복사하고 거기서 실행하십시오.

그만큼 ABC.txt 프로그램에 사용되는 파일에는 다음이 포함됩니다. 

Hello Geeks. Explaining read() method 

출력 :  

Hello Geeks. Explaining read() method 
    getLineNumber() : java.io.LineNumberInputStream.getLineNumber() 현재 줄의 수를 반환합니다. 
    구문: 
 public int getLineNumber()   Parameters :    -------   Return :    no. of current line 

구현 :  

Java
   // Java program illustrating the working of getLineNumber() method   import     java.io.*  ;   public     class   NewClass   {      public     static     void     main  (  String  []     args  )     throws     IOException      {      // LineNumberInputStream & FileInputStream initially null      LineNumberInputStream     geekline     =     null  ;      FileInputStream     geekinput     =     null  ;      try      {      char     c  ;      int     a       b  ;      // New InputStream : 'ABC' is created      geekinput     =     new     FileInputStream  (  'ABC.txt'  );      geekline     =     new     LineNumberInputStream  (  geekinput  );      while  ((  a     =     geekline  .  read  ())     !=     -  1  )      {      // So we convert each integer value to char      c     =     (  char  )  a  ;          // Use of getLineNumber() : to print line no.      a     =     geekline  .  getLineNumber  ();      System  .  out  .  println  (  ' At line : '     +     a  );      System  .  out  .  print  (  c  );      }      a     =     geekline  .  getLineNumber  ();      System  .  out  .  println  (  ' at line: '     +     a  );      }      catch  (  Exception     e  )      {      // In case of error      e  .  printStackTrace  ();      System  .  out  .  println  (  'ERROR Occurs '  );      }      finally      {      // Closing the streams Once the End of Input Stream is reached      if  (  geekinput     !=     null  )      geekinput  .  close  ();         if  (  geekline     !=     null  )      geekline  .  close  ();      }      }   }   

메모 : 
온라인 IDE의 파일에 액세스할 수 없으므로 다음 Java 코드는 여기서 실행되지 않습니다. 
따라서 프로그램을 시스템에 복사하고 거기서 실행하십시오.

그만큼 ABC.txt 프로그램에 사용되는 파일에는 다음이 포함됩니다. 

no. of lines 

출력 :  

 At line : 0 n At line : 0 o At line : 0 . At line : 0 At line : 0 o At line : 0 f At line : 1 At line : 1 l At line : 1 i At line : 1 n At line : 1 e At line : 1 s at line: 1 
    사용 가능() : java.io.LineNumberInputStream.available() 차단하지 않고 입력 스트림에서 읽을 수 있는 바이트 수를 반환합니다. 
    구문: 
public int available()   Parameters :    -------   Return :    returns the no. of bytes that can be read from the Input Stream.   Exception:    IOException : in case I/O error occurs 

구현 :  

Java
   // Java program illustrating the working of available() method   import     java.io.*  ;   public     class   NewClass   {      public     static     void     main  (  String  []     args  )     throws     IOException      {      // LineNumberInputStream & FileInputStream initially null      LineNumberInputStream     geekline     =     null  ;      FileInputStream     geekinput     =     null  ;      try  {      char     c  ;      int     a       b  ;      // New InputStream : 'ABC' is created      geekinput     =     new     FileInputStream  (  'ABC.txt'  );      geekline     =     new     LineNumberInputStream  (  geekinput  );      while  ((  a     =     geekline  .  read  ())     !=     -  1  )      {      // So we convert each integer value to char      c     =     (  char  )  a  ;      // Use of available method : return no. of bytes that can be read      a     =     geekline  .  available  ();      System  .  out  .  println  (  c     +     ' Bytes available : '     +     a  );      }      }      catch  (  Exception     e  )      {      // In case of error      e  .  printStackTrace  ();      System  .  out  .  println  (  'ERROR Occurs '  );      }      finally      {      // Closing the streams Once the End of Input Stream is reached      if  (  geekinput     !=     null  )      geekinput  .  close  ();      if  (  geekline     !=     null  )      geekline  .  close  ();      }      }   }   

메모 : 
온라인 IDE의 파일에 액세스할 수 없으므로 다음 Java 코드는 여기서 실행되지 않습니다. 
따라서 프로그램을 시스템에 복사하고 거기서 실행하십시오.

그만큼 ABC.txt 프로그램에 사용되는 파일에는 다음이 포함됩니다. 

available 

출력 :  

a Bytes available : 4 v Bytes available : 3 a Bytes available : 3 i Bytes available : 2 l Bytes available : 2 a Bytes available : 1 b Bytes available : 1 l Bytes available : 0 e Bytes available : 0 
    setLineNumber() : java.io.LineNumberInputStream.setLineNumber(int arg) 우리가 원하는 인수에 줄 번호를 할당합니다. 
    구문: 
public void setLineNumber(int arg)   Parameters :    arg : line number to assign   Return :    void   Exception:    ----- 

구현 :  

Java
   // Java program illustrating the working of setLineNumber() method   import     java.io.*  ;   public     class   NewClass   {      public     static     void     main  (  String  []     args  )     throws     IOException      {      // LineNumberInputStream & FileInputStream initially null      LineNumberInputStream     geekline     =     null  ;      FileInputStream     geekinput     =     null  ;      try  {      char     c  ;      int     a       b     =     0  ;      // New InputStream : 'ABC' is created      geekinput     =     new     FileInputStream  (  'ABC.txt'  );      geekline     =     new     LineNumberInputStream  (  geekinput  );      while  ((  a     =     geekline  .  read  ())     !=     -  1  )      {      // So we convert each integer value to char      c     =     (  char  )  a  ;          // Use of setLineNumber() : to set the line no.      geekline  .  setLineNumber  (  100     +     b  );      // getLineNumber() : returning the current line no.      a     =     geekline  .  getLineNumber  ();      System  .  out  .  println  (  c     +     ' Line No. Set : '     +     a  );      b  ++  ;      }      }      catch  (  Exception     e  )      {      // In case of error      e  .  printStackTrace  ();      System  .  out  .  println  (  'ERROR Occurs '  );      }      finally      {      // Closing the streams Once the End of Input Stream is reached      if  (  geekinput     !=     null  )      geekinput  .  close  ();      if  (  geekline     !=     null  )      geekline  .  close  ();      }      }   }   

메모 : 
온라인 IDE의 파일에 액세스할 수 없으므로 다음 Java 코드는 여기서 실행되지 않습니다. 
따라서 프로그램을 시스템에 복사하고 거기서 실행하십시오.

그만큼 ABC.txt 프로그램에 사용되는 파일에는 다음이 포함됩니다. 

LineNumber 

출력 :  

L Line No. Set : 100 i Line No. Set : 101 n Line No. Set : 102 e Line No. Set : 103 N Line No. Set : 104 u Line No. Set : 105 m Line No. Set : 106 b Line No. Set : 107 e Line No. Set : 108 r Line No. Set : 109 
    건너뛰기() : java.io.LineNumberInputStream.skip(긴 인수) 입력 스트림 데이터에서 'arg' 바이트를 건너뛰고 삭제합니다. LineNumberInputStream의 건너뛰기 메서드는 바이트 배열을 만든 다음 n 바이트를 읽거나 스트림 끝에 도달할 때까지 이를 반복적으로 읽습니다. 
    구문: 
public long skip(long arg)   Parameters :    arg : no. of bytes of Input Stream data to skip.   Return :    no. of bytes to be skipped   Exception:    IOException : in case I/O error occurs 

구현 :  

Java
   // Java program illustrating the working of setLineNumber() method   import     java.io.*  ;   public     class   NewClass   {      public     static     void     main  (  String  []     args  )     throws     IOException      {      // LineNumberInputStream & FileInputStream initially null      LineNumberInputStream     geekline     =     null  ;      FileInputStream     geekinput     =     null  ;      try  {      char     c  ;      int     a       b     =     0  ;      // New InputStream : 'ABC' is created      geekinput     =     new     FileInputStream  (  'ABC.txt'  );      geekline     =     new     LineNumberInputStream  (  geekinput  );      while  ((  a     =     geekline  .  read  ())     !=     -  1  )      {      // So we convert each integer value to char      c     =     (  char  )  a  ;      // skip() : to skip and discard 'arg' bytes      // Here skip() will skip and discard 3 bytes.      geekline  .  skip  (  3  );      System  .  out  .  println  (  c  );      }      }      catch  (  Exception     e  )      {      // In case of error      e  .  printStackTrace  ();      System  .  out  .  println  (  'ERROR Occurs '  );      }      finally  {      // Closing the streams Once the End of Input Stream is reached      if  (  geekinput     !=     null  )      geekinput  .  close  ();      if  (  geekline     !=     null  )      geekline  .  close  ();      }      }   }   

메모 : 
온라인 IDE의 파일에 액세스할 수 없으므로 다음 Java 코드는 여기서 실행되지 않습니다. 
따라서 프로그램을 시스템에 복사하고 거기서 실행하십시오.

그만큼 ABC.txt 프로그램에 사용되는 파일에는 다음이 포함됩니다. 

Program Explaining Skip() method 

출력 :

P r E a n k ) t 
    read() : java.io.LineNumberInputStream.read(byte[] buffer int offset int maxlen) 입력 스트림에서 최대 'maxlen' 바이트를 바이트로 읽습니다. 
    구문: 
public int read(byte[] buffer int offset int maxlen)   Parameters :    buffer : buffer whose data to read offset : starting offset of the data maxlen : max. no. of bytes to read   Return :    total no. of bytes else return -1 if End of Input Stream is identified   Exception:    IOException : in case I/O error occurs 

구현 :  

Java
   // Java program illustrating the working of read() method   import     java.io.*  ;   public     class   NewClass   {      public     static     void     main  (  String  []     args  )     throws     IOException      {      // LineNumberInputStream & FileInputStream initially null      LineNumberInputStream     geekline     =     null  ;      FileInputStream     geekinput     =     null  ;      try  {      char     c  ;      int     a  ;      // New InputStream : 'ABC' is created      geekinput     =     new     FileInputStream  (  'ABC.txt'  );      geekline     =     new     LineNumberInputStream  (  geekinput  );      // read() method returning Bytes of Input Stream as integer      // '-1' indicating to read till end Of Input Stream      while  ((  a  =  geekline  .  read  ())  !=-  1  )      {      // Since read() method returns Integer value      // So we convert each integer value to char      c     =     (  char  )  a  ;      System  .  out  .  print  (  c  );      }      }      catch  (  Exception     e  )      {      // In case of error      e  .  printStackTrace  ();      System  .  out  .  println  (  'ERROR Occurs '  );      }      finally      {      // Closing the streams Once the End of Input Stream is reached      if  (  geekinput     !=     null  )      geekinput  .  close  ();      if  (  geekline     !=     null  )      geekline  .  close  ();      }      }   }   

메모 : 
온라인 IDE의 파일에 액세스할 수 없으므로 다음 Java 코드는 여기서 실행되지 않습니다. 
따라서 프로그램을 시스템에 복사하고 거기서 실행하십시오.

그만큼 ABC.txt 프로그램에 사용되는 파일에는 다음이 포함됩니다. 

Read() method 

메소드가 하는 일은 offset = r이고 maxlen = 5입니다... 그래서 ---즉. 3 오프셋 다음 5 바이트, 즉 Read( 그런 다음 다시 오프셋하므로 -- 
출력 :  

The number of char read: 5 ---Read(-- 
    mark() : Java.io.LineNumberInputStream.mark(int arg) 입력 스트림의 현재 위치를 표시합니다. readlimit, 즉 마크 위치가 유효하지 않게 되기 전에 읽을 수 있는 최대 바이트 수를 설정합니다. 
    구문:
public void mark(int arg)   Parameters :   arg : integer specifying the read limit of the input Stream   Return :   void 
    재설정() : Java.io.LineNumberInputStream.reset() mark() 메소드에 의해 호출됩니다. 입력 스트림을 표시된 위치로 다시 배치합니다. 
    구문:
public void reset()   Parameters :   ----   Return :   void   Exception :   ->   IOException :   If I/O error occurs. 

LineNumberInputStream 클래스 메소드를 설명하는 Java 프로그램: Reset() 및 mark() 

Java
   // Java program illustrating the working of LineNumberInputStream method   // mark() and reset()   import     java.io.*  ;   public     class   NewClass   {      public     static     void     main  (  String  []     args  )     throws     Exception      {      LineNumberInputStream     geekline     =     null  ;      FileInputStream     geek     =     null  ;      try  {      geek     =     new     FileInputStream  (  'GEEKS.txt'  );      geekline     =     new     LineNumberInputStream  (  geek  );      // read() method : reading and printing Characters one by one      System  .  out  .  println  (  'Char : '     +     (  char  )  geekline  .  read  ());      System  .  out  .  println  (  'Char : '     +     (  char  )  geekline  .  read  ());      System  .  out  .  println  (  'Char : '     +     (  char  )  geekline  .  read  ());      // mark() : read limiting the 'geek' input stream      geekline  .  mark  (  0  );      // skip() : it results in reading of 'e' in G'e'eeks      geekline  .  skip  (  1  );      System  .  out  .  println  (  'skip() method comes to play'  );      System  .  out  .  println  (  'mark() method comes to play'  );      System  .  out  .  println  (  'Char : '     +     (  char  )  geekline  .  read  ());      System  .  out  .  println  (  'Char : '     +     (  char  )  geekline  .  read  ());      System  .  out  .  println  (  'Char : '     +     (  char  )  geekline  .  read  ());      boolean     check     =     geekline  .  markSupported  ();      if  (  geekline  .  markSupported  ())      {      // reset() method : repositioning the stream to marked positions.      geekline  .  reset  ();      System  .  out  .  println  (  'reset() invoked'  );      System  .  out  .  println  (  'Char : '     +     (  char  )  geekline  .  read  ());      System  .  out  .  println  (  'Char : '     +     (  char  )  geekline  .  read  ());      }      else      {      System  .  out  .  println  (  'reset() method not supported.'  );      }      System  .  out  .  println  (  'geekline.markSupported() supported reset() : '         +     check  );      }      catch  (  Exception     except  )      {      // in case of I/O error      except  .  printStackTrace  ();      }      finally      {      // releasing the resources back to the GarbageCollector when closes      if  (  geek     !=     null  )      geek  .  close  ();      if  (  geekline     !=     null  )      geekline  .  close  ();      }      }   }   

메모 :  
이 코드는 여기에 해당 파일이 없기 때문에 온라인 IDE에서 실행되지 않습니다. 
시스템에서 이 코드를 실행하여 작동 여부를 확인할 수 있습니다. 

ABC.txt 코드에 사용된 파일에는 

HelloGeeks 

출력 : 

Char : H Char : e Char : l skip() method comes to play mark() method comes to play Char : o Char : G Char : e reset() method not supported. geekline.markSupported() supported reset() : false 

 
 

퀴즈 만들기