分别前后用Read(byte[])(记作R1)放法,和Read(byte[] b,int off,int len)(记作R2)读取文件内容。先后设置将R1()读取的内容放到缓冲buffer1,将R2放到buffer2,输出结果显示的只有buffer1的数据。假如它们的先后顺序调转了,结果是两者被输出!为什么这样的?请求大大们解答。
import java.io.*;public class fis{
public static void main(String[] args) throws IOException {
File file=new File("fis.java");
if(file.exists()){
FileInputStream fs=new FileInputStream(file);
int len=fs.available();
byte buffer1[]=new byte[len];
byte buffer2[]=new byte[len];
//fs.read(buffer2,0, 6);
//String f2=new String(buffer2);
fs.read(buffer1);
String f1=new String(buffer1);
fs.read(buffer2,0, 6);
String f2=new String(buffer2);
System.out.println(f1);
System.out.println("===========");
System.out.println("截取的部分内容:"+f2);
}
else System.out.println("文件不存在或读取错误!");
}
}
import java.io.*;public class fis{
public static void main(String[] args) throws IOException {
File file=new File("fis.java");
if(file.exists()){
FileInputStream fs=new FileInputStream(file);
int len=fs.available();
byte buffer1[]=new byte[len];
byte buffer2[]=new byte[len];
//fs.read(buffer2,0, 6);
//String f2=new String(buffer2);
fs.read(buffer1);
String f1=new String(buffer1);
fs.read(buffer2,0, 6);
String f2=new String(buffer2);
System.out.println(f1);
System.out.println("===========");
System.out.println("截取的部分内容:"+f2);
}
else System.out.println("文件不存在或读取错误!");
}
}
解决方案 »
- 给定一字符数组,求数组中字符组成的所有排列?
- td里面 table要靠上贴着上面放怎么写?
- java中如何标志一个方法为过时方法?
- 再次求救!!!AES加密算法问题
- 为什么这个程序不加构造就会报错呢????
- 对象和实例有什么区别?Class c =Class.forName("ss.dd.bb.Bean");和Bean b = new Bean();有什么区别?
- 大哥哥们,北京丫头问一个问题希望大家不要见笑·····
- java读取ini文件
- 请教:JAVA中的数据传输问题?(100分相送)
- 运行的时候 说这个方法报空指针啊 求大神解答!!!
- JNDI资源绑定
- 请教: 关于(tempsql.length()!=0)与(!"".equals(tempsql))是否相同
fs.read(buffer2,0, 6); 的意思就是读前六个字节从buffer2的0位子开始原因就是你使用了int len=fs.available();
byte buffer1[]=new byte[len];
使一次全部读完了!