62,633
社区成员
发帖
与我相关
我的任务
分享
public class Test {
public static void main(String[] args) {
System.out.println(read(3,9));
}
public static String read(int from ,int to){
String result="";
byte[] result2=new byte[to-from+1];
try{
FileInputStream fis=new FileInputStream("d:\\ss.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
bis.skip(from-1);
bis.read(result2, 0, to-from+1);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return new String(result2);
}
}
RandomAccessFile r = new RandomAccessFile(new File("c:/1.txt", "r"));//只读方式打开文件
r.seek(100);//指定下一次的开始位置
byte[] bs = new byte[1024];
r.read(bs);
r.readChar();
r.readInt();//读取一个整数
public class Test {
public static void main(String[] args) {
System.out.println(read(3,9));
}
public static String read(int from ,int to){
String result="";
try{
FileInputStream fis=new FileInputStream("d:\\ss.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
bis.skip(from-1);
int c=0;
for(int i=0;(i<to-from)&&(c=bis.read())!=-1;i++){
result+=(char)c;
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return result;
}
}