java中详细讲解数据流操作,包括数据及文件的传输Reader reader- nulltrySystem. out. println("以字符为单位读取文件内容,一次读一个字节:");次读一个字符reader= new InputStreamReader (new FileInputStream(file))int tcmpcharhile ((tempchar reader readO)1){/对于 windows下,\rn这两个字符在一起时,表示个换行。//但如果这两个字符分开显示时,会换两次行∥因此,屏蔽掉\r,或者屏蔽\n。否则,将公多出很多空行。if(((char) tempchar)!-\r')System. out. print((char) tempchar)reader. close ocatch (Exception e)(e printstackTrace Otry iSystem. out. println("以字符为单位读取文件内容,一次读多个字节:次读多个字符charl] tempchars new char [30]int charred=0reader= new InputStreamReader (new FileInputStream(fileName))读入多个字符到字符数组中, charred为一次读取字符数((while ((ccharred= reader read(tempchars))!=1)[/同样屏蔽掉\r不显示if((charrcad--tcmpchars length&&(tempchars [tempchars length -1]!\r))1Systnt(tempchars)for (int0: i< charred; i++)f( tempchars i=’\r3)continueelseSystem. out. print(tempchars Li])y catch (Exception e1)e1 printStackTrace Ofinallif (reader !=nu11)ttryreader. closecatch (IOException e1)i*以行为单位读取文件,常用于读面向行的格式化文件分区java基础的第3页public static void rcadFilcByLines(String fileName)tFile file new File(fileName)BufferedReader reader nulltrySystem. out. println("以行为单位读取文什内容,一次读一整行:")reader- new BuffcrcdRcadcr (new FilcRcadcr(filc))String tempString- nullnt⊥ine=1//一次读入一行,直到读入nu11为文件结束whilc ((tcmpString- reader. rcadLinco)!nu11)/显小行号System. out. println( line line +: tempStringline+reader. close ocatch (IOException e)te printstackTrace Og finallyif (reader nulltryredIder. closecatch(IOException e1)(随机读取文件内容水public static void readFileBy RandomAccess(String fileName)Randomaccessfile randomfile= nulltrySystem. out. println("随机读取一段文件内容:"打丌一个随机访问文件流,按只读方式randomFile= new RandomAccess File(fileName, r")//文件长度,字节数long fileLength randomFile length O/读文件的起始位置int beginIndex=(fileLength >4)? 4:0将读文件的开始位置移到 beginIndex位置。randomFile. seek(beginIndex)by te bytbyte[10]int bvteread =0//一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。/将一次读取的字节数赋给 bytorcadwhile((byteread randomFile read(bytes))System. out. write(bytes, 0, byteread)y catch (IOExccption ce printStackTrace Ofinallif (randomFile != nu11)(tryrandomFile. closei catch (IOException e1)I分区java基础的第4页*显小输入流中还剩的字节数水private static void showAvailableBytes(Input Stream in) ttry tSystem.out. println("当前字节输入流中的字节数为:"+in. available0)g catch (lOExcepe printstackTrace opublic static void main(String[] args)IString fileName ="C: temp/ newTemptxtRcadFromFilc. rcadFilcBy Bytes(fileName)ReadFromFile. readFileByChars(fileName)ReadFromFile. readFileByLines (fileName)ReadFromFile readFile ByRandomAccess(fileName)、将内容追加到文件尾部public class AppendToFile I來A方法追加文件:使用 RandomAccessfilepublic static void appendMcthodA (String filcNamc, String content)try I//打开一个随机访问文件流,按读写方式RandomAccessfile randomFile= new RandomAccessFile(fileName,rw")//文件长度,字节数long fileLength randomFile length/将写文件指针移到文件尾k(filelength)randomFile. writeBytes(content)randomFilc. close1 catch (IOException e)ie. prinklraceo/宋水*B方法追加文件:使用 FileWriter分区java基础的第5页public static void appendMcthodB (String filcNamc, String content)Itry i//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件FileWriter writer= new FileWriter(fileName, truewriter. write(contentwriter. closc (3 catch (IOException e)ie. printStacklraceo)public static void main(String[] args) fString content= new append/按方法A追加文件AppendToFile. appendMethodA (fileName, content)AppendToFile. appendMethodA (fileName, append end. \ n")显示文件内容RcadFromFilc. rcadFilcByLincs(filcNamc)//按方法B追加文件AppendToFile. appendMethodB(fileName, content)appendtoFildMethodB(fileNamed end/显示文件内容ReadFromFile. readFileBy Lines(fileName)分区java基础的第6页