本文共 3991 字,大约阅读时间需要 13 分钟。
一直以来流都是困扰新手和才工作者的东东,这里也算给新来者一些简介。用最简单的语言,说明罪简单的道理。希望如此
package stream;import java.io.IOException;import java.io.InputStream;/** * Created by ycy on 16/3/5. * inputStream 输入流主要方法 * read * skip(网络中一般很少用) * available * close */public class InputStream_test { public static void main(String[] args) throws IOException { InputStream in=null;//没有实现 /*单字节读取*/ byte[] input=new byte[10]; for (int i = 0; i
/** * 单字节输入 * * @param out * @throws IOException */ public static void generateCharacters(OutputStream out) throws IOException { int fistPrintableCharacter = 33; int numberOfPrintableCharacters = 94; int numberOfCharactersPerLine = 72; int start = fistPrintableCharacter; while (true) { /*无限循环*/ for (int i = start; i < start + numberOfCharactersPerLine; i++) { out.write(( (i - fistPrintableCharacter) % numberOfCharactersPerLine) + fistPrintableCharacter); } out.write('\r');//回车 out.write('\n');//换行 start = ((start + 1) - fistPrintableCharacter) % numberOfCharactersPerLine + fistPrintableCharacter; } } /** * 有数组缓存测输入流 * * @param out * @throws IOException */ public static void getnerateCharacters(OutputStream out) throws IOException { int fistPrintableCharacter = 33; int numberOfPrintableCharacters = 94; int numberOfCharactersPerLine = 72; int start = fistPrintableCharacter; byte[] line = new byte[numberOfCharactersPerLine + 2]; //+2 对应回车和换行 while (true) { for (int i = start; i < start + numberOfCharactersPerLine; i++) { line[i - start] = (byte) ((i - fistPrintableCharacter) % numberOfPrintableCharacters + fistPrintableCharacter); } line[72] = (byte) '\r'; line[72] = (byte) '\n'; try { out.write(line); } catch (IOException ex) { System.out.println(ex.getMessage()); } out.write(line); start = ((start + 1) - fistPrintableCharacter) % numberOfPrintableCharacters + fistPrintableCharacter; } }
DataOutputStream dataout = new DataOutputStream( new BufferedOutputStream( new FileOutputStream("data.txt")));
//读取input流的阅读器/** * 使用reader 字节变为字符 * 使用bufferreader 使用缓存加快了速度 * @param in * @return * @throws IOException */public static String getMaccyrillicString(InputStream in) throws IOException{ Reader re=new InputStreamReader(in,"macCyrillic"); re=new BufferedReader(re,1024); StringBuilder sb=new StringBuilder(); int c; while ((c= re.read())!=-1){ sb.append((char)c); } return sb.toString();}public static void main(String[] args) {}
转载地址:http://koofa.baihongyu.com/