byte []b=new byte[2048]; int num=din.read(b); // 将输入的数据读到一个数组中 循环判断num的值 要是为-1的话则清空了 否则还有数据
JAVA自己不是有垃圾回收机制吗?
不是的,搞错了,我第一次输入了一些数据,我在第二次输入之前想把以前的数据清空掉,这怎么清?如我第一次用system.in.read()输入了一字符,第二次用System.in.read(buffer)的时候就不要我输入 ,就把后面的字符打印出来了,请问怎样在第二次输入之前把输入缓冲区清掉,如下的代码:import java.io.*;public class ReadString{ public static void main(String[] args) throws IOException { int counter = 0; byte buffer[] = new byte[512];
printStar(); System.out.println("The length of buffer array is: " + buffer.length); printStar();
//Test read a char System.out.println(); printStar(); System.out.print("Please enter a char:"); System.out.println("print the input char is:" + (char)(System.in.read())); printStar();
//Test read a string System.out.println(); printStar(); System.out.print("Please enter a string:"); counter = System.in.read(buffer); System.out.println("The length of the input string is:" + counter); System.out.print("The input string is:"); for (int i=0; i<counter; i++) { System.out.print(buffer[i] + " "); }
}
public static void printStar() { System.out.println("***************************************"); } } 结果: *************************************** The length of buffer array is: 512 ****************************************************************************** Please enter a char:g print the input char is:g ****************************************************************************** Please enter a string:The length of the input string is:2//这里我没有输入,输入数组的长度就为2了 The input string is:13 10
System.in会把你输入完后摁的回车换行也做为两个字符在输入缓冲区中,所以直接不用你输!你可以再你下一次输入之前读出来回车和换行符! 代码如下://Test read a char System.out.println(); printStar(); System.out.print("Please enter a char:"); System.out.println("print the input char is:" + (char)(System.in.read())); printStar(); System.in.read(); System.in.read();//Test read a string System.out.println(); printStar(); System.out.print("Please enter a string:"); counter = System.in.read(buffer); System.out.println("The length of the input string is:" + counter); System.out.print("The input string is:"); for (int i=0; i <counter; i++) { System.out.print(buffer[i] + " "); } } public static void printStar() { System.out.println("***************************************"); } } 输出结果如下: *************************************** The length of buffer array is: 512 ****************************************************************************** Please enter a char:h print the input char is:h ****************************************************************************** Please enter a string:d The length of the input string is:3 The input string is:100 13 10注意,此时虽然你好像只输入了一个d,但里面有三个,13和10分别为回车 和 换行的ASCII码
如果清空输入缓冲区的话,可以循环读啊!知道返回 -1
int num=din.read(b); // 将输入的数据读到一个数组中
循环判断num的值 要是为-1的话则清空了 否则还有数据
public static void main(String[] args) throws IOException
{
int counter = 0;
byte buffer[] = new byte[512];
printStar();
System.out.println("The length of buffer array is: " + buffer.length);
printStar();
//Test read a char
System.out.println();
printStar();
System.out.print("Please enter a char:");
System.out.println("print the input char is:" + (char)(System.in.read()));
printStar();
//Test read a string
System.out.println();
printStar();
System.out.print("Please enter a string:");
counter = System.in.read(buffer);
System.out.println("The length of the input string is:" + counter);
System.out.print("The input string is:");
for (int i=0; i<counter; i++)
{
System.out.print(buffer[i] + " ");
}
}
public static void printStar()
{
System.out.println("***************************************");
}
}
结果:
***************************************
The length of buffer array is: 512
******************************************************************************
Please enter a char:g
print the input char is:g
******************************************************************************
Please enter a string:The length of the input string is:2//这里我没有输入,输入数组的长度就为2了
The input string is:13 10
代码如下://Test read a char
System.out.println();
printStar();
System.out.print("Please enter a char:");
System.out.println("print the input char is:" + (char)(System.in.read()));
printStar();
System.in.read();
System.in.read();//Test read a string
System.out.println();
printStar();
System.out.print("Please enter a string:");
counter = System.in.read(buffer);
System.out.println("The length of the input string is:" + counter);
System.out.print("The input string is:");
for (int i=0; i <counter; i++)
{
System.out.print(buffer[i] + " ");
} } public static void printStar()
{
System.out.println("***************************************");
}
} 输出结果如下:
***************************************
The length of buffer array is: 512
******************************************************************************
Please enter a char:h
print the input char is:h
******************************************************************************
Please enter a string:d
The length of the input string is:3
The input string is:100 13 10注意,此时虽然你好像只输入了一个d,但里面有三个,13和10分别为回车 和 换行的ASCII码