对 byte 有了新的认知;byte表示一个字节 因此他可以表示成 一个 数字; 一个数字可以映射成一个字符;InputStream 中的 read() 函数 的意思是读入一个整数,读入一个整数的意思就是读入一个字节;而 read( byte b[] ) 是读入整个文档内容,可以这么认为 read是把 文本里面的所有数字读进去了;所以只要控制好 read() 函数就可以实现 一行一行读入了; 为什么write()函数写到文档里面不是数字 而是字符呢! 用 System.out.pirntln() 函数打印出来发现确实数字; 其实数字就是字符 字符就是数字,可以把那一堆字符看成数字;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.InputStream;
import java.util.Random;
//import java.io.StringBufferInputStream;
public class Number1 {
public static void main( String arg[] )throws Exception {
Random rad = new Random();
File file = new File("D:"+File.separator+"tt.txt");
file.createNewFile();
OutputStream out = new FileOutputStream(file);
for( int i = 100; i >= 0; i-- )
{
int n = rad.nextInt(4)+1; StringBuffer str = new StringBuffer();
for( int j = 0; j < n; j++ )str.append((char)(rad.nextInt(26) + 'a'));
str.append("\r\n");
out.write(str.toString().getBytes());
}
File file2 = new File("D:"+File.separator+"xx.txt");
file2.createNewFile();
OutputStream output = new FileOutputStream(file2);
InputStream in = new FileInputStream(file);
byte b[] = new byte[100]; int sum = 0;
int len = (int)file.length();
for( int i = 0; i < len; i++ ){
byte temp = (byte)in.read();
if( temp != (byte)'\r' && temp != (byte)'\n' )
b[sum++] = temp;
else{
output.write( new String(b,0,sum).getBytes() ); sum = 0;
if( temp == (byte)'\r') output.write( (byte)'\r' );
else output.write( (byte)'\n');
}
}
in.close();
out.close();
}
}