📌  相关文章
📜  Java中的 BufferedInputStream skip(long) 方法及示例

📅  最后修改于: 2022-05-13 01:55:05.996000             🧑  作者: Mango

Java中的 BufferedInputStream skip(long) 方法及示例

Java中BufferedInputStream类的skip(long)方法用于从缓冲的输入流中跳过 n 个字节的数据。跳过的字节数以长类型存储和返回。终止条件涉及以下两者之一:

  • 要么读入一个字节数组,直到覆盖 n 个字节,要么,
  • 当输入结束时,流被满足。

但是,如果传递了负值,则不会发生跳过。
句法:

public long skip(long n)

参数:此方法接受长类型的n ,表示需要从输入流中跳过的字节数。
返回值:该方法以long类型返回跳过的字节数。
异常:如果此输入流已通过调用 close() 方法关闭,或者该流不支持查找,或者发生任何其他 I/O 错误,则此方法抛出IOException
下面的程序说明了 IO 包中 BufferedInputStream 类中的 skip(long) 方法:
程序1:假设文件“c:/demo.txt”存在。

Java
// Java program to illustrate
// BufferedInputStream skip(long) method
import java.io.*;
public class GFG {
    public static void main(String[] args)
    {
    
        // Create input stream 'demo.txt'
        // for reading containing text "GEEK"
        FileInputStream inputStream = 
        new FileInputStream("c:/demo.txt");
    
        // Convert inputStream to 
        // bufferedInputStream
        BufferedInputStream buffInputStr 
              = new BufferedInputStream(
                          inputStream);
          
        // Read until a single
        // byte is available
        while(buffInputStr.available()>0) {
          
            // Skip single byte from the stream
            buffInputStr.skip(1);
          
            // Read next available byte and
            // convert to char
            char c = (char) buffInputStr.read();
          
            // Print character
            System.out.print(" " + c);
         }
    }
}


Java
// Java program to illustrate
// BufferedInputStream skip(long) method
import java.io.*;
public class GFG {
    public static void main(String[] args)
    {
    
        // Create input stream 'demo.txt'
        // for reading containing text "GEEKSFORGEEKS"
        FileInputStream inputStream = 
        new FileInputStream("c:/demo.txt");
    
        // convert inputStream to 
        // bufferedInputStream
        BufferedInputStream buffInputStr
            = new BufferedInputStream(
                        inputStream);
          
        // Read until a single
        // byte is available
        while(buffInputStr.available()>0) {
          
            // Skip single byte from the stream
            buffInputStr.skip(3);
          
            // Read next available byte and
            // convert to char
            char c = (char) buffInputStr.read();
          
            // Print character
            System.out.print(" " + c);
         }
    }
}


输出:
E K

程序2:假设文件“c:/demo.txt”存在。

Java

// Java program to illustrate
// BufferedInputStream skip(long) method
import java.io.*;
public class GFG {
    public static void main(String[] args)
    {
    
        // Create input stream 'demo.txt'
        // for reading containing text "GEEKSFORGEEKS"
        FileInputStream inputStream = 
        new FileInputStream("c:/demo.txt");
    
        // convert inputStream to 
        // bufferedInputStream
        BufferedInputStream buffInputStr
            = new BufferedInputStream(
                        inputStream);
          
        // Read until a single
        // byte is available
        while(buffInputStr.available()>0) {
          
            // Skip single byte from the stream
            buffInputStr.skip(3);
          
            // Read next available byte and
            // convert to char
            char c = (char) buffInputStr.read();
          
            // Print character
            System.out.print(" " + c);
         }
    }
}
输出:
K R K

参考:
https://docs.oracle.com/javase/10/docs/api/java Java)