请问Java InputStream中read()与read(byte[],int,int)有联系吗,有没有谁实现时调用了对方,为什么我这两个函数一起用时会出错呢?
public class LowerCaseInputStream extends FilterInputStream { public LowerCaseInputStream(InputStream in) 
{
super(in);
}
 
public int read() throws IOException {
int c = super.read();
return (c == -1 ? c : Character.toLowerCase((char)c));
}

public int read(byte[] b, int offset, int len) throws IOException {
int result = super.read(b, offset, len);
for (int i = offset; i < offset+result; i++) {
b[i] = (byte)Character.toLowerCase((char)b[i]);
}
return result;
}
}import java.io.FileInputStream;
import java.util.Scanner;
import java.io.*;
public class Test
{
public static void main(String[] args) throws IOException
{
File file = new File("text.txt");
if(!file.exists())
{
file.createNewFile();
}
FileOutputStream output = new FileOutputStream(file);
String string = new String();
Scanner in = new Scanner(System.in);//从键盘输入字符串
string = in.nextLine();
output.write(string.getBytes());//将字符串写入文件
output.flush();
output.close();
FileInputStream input = new FileInputStream(file);
/*byte[] c = new byte[30];
int m = input.read(c, 2, 3);
System.out.println((char)c[2]);
System.out.println(c.length);
for(int i=2;i<m+2;i++)
{
System.out.print((char)c[i]);
}*/
LowerCaseInputStream another_in = new LowerCaseInputStream(input);//包装FileInputStream
LowerCaseInputStream another_in1 = new LowerCaseInputStream(input);
byte[] arr = new byte[20];
int i;
while((i=another_in1.read())!=-1)
{
char c = (char)i;
System.out.print(c);
}
int j = another_in.read(arr,2,3);
for(i=2;i<2+j;i++)
{
System.out.print((char)arr[i]);
}
another_in.close();
input.close();
}}

解决方案 »

  1.   

    两个函数能一起运行,我帮你debug了,你看一下,下面代码能运行的
    import java.io.FileInputStream; 
    import java.util.Scanner; 
    import java.io.*; class LowerCaseInputStream extends FilterInputStream { public LowerCaseInputStream(InputStream in) 

    super(in); 
    } public int read() throws IOException { 
    int c = super.read(); 
    return (c == -1 ? c : Character.toLowerCase((char)c)); 
    } public int read(byte[] b, int offset, int len) throws IOException { 
    int result = super.read(b, offset, len); 
    for (int i = offset; i < offset+result; i++) { 
    b[i] = (byte)Character.toLowerCase((char)b[i]); 

    return result; 


    public class Test 

    public static void main(String[] args) throws IOException 

    File file = new File("text.txt"); 
    if(!file.exists()) 

    file.createNewFile(); 

    FileOutputStream output = new FileOutputStream(file); 
    String string = new String(); 
    Scanner in = new Scanner(System.in);//从键盘输入字符串 
    string = in.nextLine(); 
    output.write(string.getBytes());//将字符串写入文件 
    output.flush(); 
    output.close(); 
    FileInputStream input = new FileInputStream(file); 
    byte[] c = new byte[30]; 
    int m = input.read(c, 2, 3); 
    System.out.println((char)c[2]); 
    System.out.println(c.length); 
    for(int i=2;i <m+2;i++) 

    //System.out.print((char)c[i]); 
    System.out.print(c[i]); 
    }
    LowerCaseInputStream another_in = new LowerCaseInputStream(input);//包装FileInputStream 
    LowerCaseInputStream another_in1 = new LowerCaseInputStream(input); 
    byte[] arr = new byte[20]; 
    int i; 
    while((i=another_in1.read())!=-1) 

    char c1 = (char)i; 
    System.out.print(c1); 

    int j = another_in.read(arr,2,3); 
    for(i=2;i <2+j;i++) 

    System.out.print((char)arr[i]); 

    another_in.close(); 
    input.close(); 
    } } 
    你看一下
      

  2.   

    read()只读一个字节
    read(byte[],int,int)读一堆字节