请问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();
}}
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();
}}
解决方案 »
- 急!JAVA如何用xpath处理XML,返回XML子集?
- 关于异常的问题,大家进来说说
- JOptionPane.showMessageDialog无法显示自定义图标
- 关于日期问题
- java中怎样调用C#写的dll
- java怎样实现文件的拷贝啊,只能用流一行一行读写吗?有没有其它更好的办法?
- 关于增量录入数据的问题,在线等候
- 帮忙看下这段代码是什么意思?
- 散分了:非科班的困惑
- 请问有谁用JAVA开发过windows下客户机/服务器系统呢?用的是哪个JAVA工具呢?方便程度比VB、PB等如何?
- A a = null; 与 A a 没有初始化有什么区别?
- 接口的作用到底是什么?~我找了个例子,帮我看看吧! (
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();
} }
你看一下
read(byte[],int,int)读一堆字节