从InputStream流中从指位置截取指定长度的流
demo01
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test {
private final static int BUFFER_SIZE = 10;
public static void main(String[] args) {
try {
int need2read = 22;
int probe = 0;
byte []data = new byte[BUFFER_SIZE];
FileInputStream fis = new FileInputStream("c:\\test.txt");
fis.skip(6);
while((probe<need2read/BUFFER_SIZE) && (fis.read(data)!=-1)) {
for(int i=0;i<data.length;i++) {
System.out.print((char)data[i]);
}
probe++;
}
//
int overage = need2read%BUFFER_SIZE;
if(overage != 0) {
int ir = fis.read(data,0,overage);
for(int i=0;i< ir;i++) {
System.out.print((char)data[i]);
}
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
demo02
public class Test {
public static void main(String[] args) {
try {
byte []data = new byte[10];
FileInputStream fis = new FileInputStream("c:\\test.txt");
fis.skip(6);
fis.read(data);
fis.close();
for(int i=0;i<data.length;i++) {
System.out.print((char)data[i]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?