java输入流主要由InputStream和Reader作为基类,而输出流主要由OutputStream和Writer作为基类。
InputStream/Reader:所有输入流的基类,前者是字节输入流,后者是字符输入流;
OutputStream/Writer:所有输出流的基类,,前者是字节输出流,后者是字符输出流。
字符流还可以使用字符串作为物理节点,用于实现从字符串读取类容,或将类容写入字符串(实际上用StringBuffer充当了字符串)的功能。实例:
String scr;
StringReader sr = new Stringreader(scr);
char[] buffer = new char[32];
try{
while(hasRead=sr.read(buffer))>0)
{
System.Out.Println(new String(buffer,o.hasRead));
}
}catch(IOException e){
}......
PrintStream 与BufferedInputStream的使用方法:
public static void main(String[] args) {
PrintStream ps=null ;
try{
FileOutputStream fos=new FileOutputStream("test.txt");
ps=new PrintStream(fos);
ps.println("普通字符");
ps.println(new IOTest());
FileInputStream in=new FileInputStream("test.txt");
BufferedInputStream bis=new BufferedInputStream(in);
byte temp[] = new byte[1024];
int len= bis.read(temp,0,1024);
System.out.println(new String(temp,0,len));
}catch(Exception e){
e.printStackTrace();
}
finally
{
ps.close();
}
}
结果:
普通字符
IOTest@497934
Bufferedreader使用方法:
public static void main(String[] args) throws Exception{
FileReader in;
BufferedReader din;
Vector<String> temp= new Vector<String>();
try{
//DataInputStream dis =new DataInputStream(new FileInputStream())
in =new FileReader("D:/Seqs.txt");
din =new BufferedReader(in);
String str;
while((str=din.readLine())!=null){
temp.add(str);
}
System.out.println("temp length"+temp.size());
}catch(Exception e){
e.printStackTrace();
}
for(int i=0;i<temp.size();i++){
String str=temp.elementAt(i);
for(int j=i+1;j<temp.size();j++){
if(str.equals(temp.elementAt(j)))
System.out.println(i+" "+j+" "+str);
}
}
}