Java.io.LineNumberInputStream Class ב-Java

Java.io.LineNumberInputStream Class ב-Java

מחלקה 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 Class

    read() : java.io.LineNumberInputStream.read() קורא בייט הבא של נתונים מזרם הקלט. הוא מחזיר ערך int המייצג את הבתים בטווח של '0 - 255'. הוא מחזיר '-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  ();      }      }   }   

הערה: 
קוד ה-Java הבא לא יפעל כאן מכיוון שאיננו יכולים לגשת לשום קובץ ב-IDE מקוון. 
אז העתק את התוכנית למערכת שלך והפעל אותה שם.

ה 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  ();      }      }   }   

הערה: 
קוד ה-Java הבא לא יפעל כאן מכיוון שאיננו יכולים לגשת לשום קובץ ב-IDE מקוון. 
אז העתק את התוכנית למערכת שלך והפעל אותה שם.

ה 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 
    available() : 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  ();      }      }   }   

הערה: 
קוד ה-Java הבא לא יפעל כאן מכיוון שאיננו יכולים לגשת לשום קובץ ב-IDE מקוון. 
אז העתק את התוכנית למערכת שלך והפעל אותה שם.

ה 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  ();      }      }   }   

הערה: 
קוד ה-Java הבא לא יפעל כאן מכיוון שאיננו יכולים לגשת לשום קובץ ב-IDE מקוון. 
אז העתק את התוכנית למערכת שלך והפעל אותה שם.

ה 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 
    skip() : java.io.LineNumberInputStream.skip(long arg) מדלג ומשליך בתים 'arg' מנתוני Input Stream. שיטת הדילוג של 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  ();      }      }   }   

הערה: 
קוד ה-Java הבא לא יפעל כאן מכיוון שאיננו יכולים לגשת לשום קובץ ב-IDE מקוון. 
אז העתק את התוכנית למערכת שלך והפעל אותה שם.

ה 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' בתים מ-Input Stream לתוך בתים. 
    תחביר: 
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  ();      }      }   }   

הערה: 
קוד ה-Java הבא לא יפעל כאן מכיוון שאיננו יכולים לגשת לשום קובץ ב-IDE מקוון. 
אז העתק את התוכנית למערכת שלך והפעל אותה שם.

ה ABC.txt הקובץ המשמש בתוכנה מכיל: 

Read() method 

מה שהשיטה עושה זה offset = r ומקסלן = 5... אז ---כלומר. 3 היסט ואז 5 בתים כלומר קרא (ואז שוב היסט אז -- 
פלט:  

The number of char read: 5 ---Read(-- 
    mark() : Java.io.LineNumberInputStream.mark(int arg) מסמן את המיקום הנוכחי של זרם הקלט. הוא מגדיר מגבלת קריאה, כלומר המספר המרבי של בתים שניתן לקרוא לפני שמיקום הסימון הופך לבלתי חוקי. 
    תחביר:
public void mark(int arg)   Parameters :   arg : integer specifying the read limit of the input Stream   Return :   void 
    reset() : Java.io.LineNumberInputStream.reset() מופעל על ידי שיטת mark() . זה ממקם מחדש את זרם הקלט למיקום המסומן. 
    תחביר:
public void reset()   Parameters :   ----   Return :   void   Exception :   ->   IOException :   If I/O error occurs. 

תוכנית Java המסבירה את שיטות המחלקה LineNumberInputStream: 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 

 
 

צור חידון