过去的文件流输出写法:Filef=newFile("/Users/buxuesong/Documents/svn_code/demo/test.txt");//OutputStreamOutputStreamoutput=newFileOutputStream(f);bytebyteVal=100;output.write(byteVal);byte[]byteBuff={0,63,127};output.write(byteBuff);output.flush();output.close();//WriterWriterwriter=newFileWriter(f,true);charcharVal='a';writer.write(charVal);char[]charBuff={'a','b','c'};writer.write(charBuff);过去文件流输入写法:Filef=newFile("/Users/buxuesong/Documents/svn_code/demo/test.txt");//InputStreamInputStreaminput=newFileInputStream(f);intintVal;while((intVal=input.read())>=0){bytebyteVla=(byte)intVal;System.out.println(byteVla);}input=newFileInputStream(f);intlength;byte[]byteBuff={1,2,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4};while((length=input.read(byteBuff))>=0){for(inti=0;i=0){charcharVal=(char)intVal;System.out.println(charVal);}reader=newFileReader(f);char[]charBuff={'a','b','c'};while((length=reader.read(charBuff))>=0){for(inti=0;i=0){charcharVal=(char)intVal;System.out.println("BufferedReader"+charVal);}}try(BufferedReaderbr=newBufferedReader(newFileReader("/Users/buxuesong/Documents/svn_code/demo/testBufferedWriter.txt"))){StringinValue;while((inValue=br.readLine())!=null){System.out.println("inValue"+inValue);}}新的输入输出写法:try(BufferedReaderbr=Files.newBufferedReader(Paths.get("/Users/buxuesong/Documents/svn_code/demo/testBufferedWriter.txt"))){StringinValue;while((inValue=br.readLine())!=null){System.out.println("Files.newBufferedReader="+inValue);}}List行=Files.readAllLines(Paths.get("/Users/buxuesong/Documents/svn_code/demo/testBufferedWriter.txt"));for(Stringline:lines)System.out.println("Files.readAllLines=="+line);try(BufferedWriterbw=Files.newBufferedWriter(Paths.get("/Users/buxuesong/Documents/svn_code/demo/testNewBufferedWriter.txt"))){for(Stringd:data){bw.write(d);bw.newLine();}}Zip压缩文件写法:publicclassZipFileTest{publicstaticvoidmain(Stringargs[]){String[]data={"Line1","Line22","Line333","Line44??44","Line55555"};try(FileSystemzipFs=openZip(Paths.get("/Users/buxuesong/Documents/svn_code/demo/myData.zip"))){copyToZip(zipFs);writeToFileInZip1(zipFs,数据);writeToFileInZip2(zipFs,数据);}catch(Exceptione){System.out.println(e.getClass().getSimpleName()+"-"+e.getMessage());}}privatestaticFileSystemopenZip(PathzipPath)throwsIOException,URISyntaxException{MapproviderProps=newHashMap<>();providerProps.put("创建","真");URIzipUri=newURI("jar:file",zipPath.toUri().getPath(),null);文件系统zipFs=FileSystems.newFileSystem(zipUri,providerProps);返回zipFs;}privatestaticvoidcopyToZip(FileSystemzipFs)throwsIOException{PathsourceFile=Paths.get("/Users/buxuesong/Documents/svn_code/demo/test.txt");//PathsourceFile=.getDefault().getPath("/Users/buxuesong/Documents/svn_code/demo/test.txt");PathdestFile=zipFs.getPath("/file1Copied.txt");Files.copy(sourceFile,destFile,StandardCopyOption.REPLACE_EXISTING);}privatestaticvoidwriteToFileInZip1(FileSystemzipFs,String[]data)throwsIOException{try(BufferedWriterbw=Files.newBufferedWriter(zipFs.getPath("/newFile1.txt"))){for(Stringd:data){bw。写(d);bw.newLine();}}}privatestaticvoidwriteToFileInZip2(FileSystemzipFs,String[]data)throwsIOException{Files.write(zipFs.getPath("/newFile2.txt"),Arrays.asList(data),Charset.defaultCharset(),StandardOpenOption。创造);}}Writeroutput类型Disk:FileWriteIn-Memory:CharArrayWriterStringWriter,写StringBuffer行为类型:BufferedWriterPrintWriterStandardOpenOption类型WRITE,APPEND写CREATE,CREATE_NEW,创建,新建DELETE_ON_CLOSE,完成后删除(临时文件)try(BufferedWriterbw=Files.newBufferedWriter(Paths.get("/Users/buxuesong/Documents/svn_code/test.txt"),StandardOpenOption.CREATE,StandardOpenOption.WRITE)){PrintWriterpw=newPrintWriter(bw);pw.write("HelloWorld!\n");pw.printf("W:%4dX:%4d\n",5,235);日历cl=Calendar.getInstance();cl.set(1987,11,23);pw.printf(Locale.US,"Ibornon%1$tB%1$tA%1$tY",cl);}catch(IOExceptione){}//输出文件HelloWorld!W:5X:235IbornonDecemberWednesday1987Readermethods//关闭流并释放与其关联的所有系统资源abstractvoidclose()//标记流中的当前位置。voidmark(intreadAheadLimit)//判断这个流是否支持mark()操作。booleanmarkSupported()//返回一个不读取任何字符的新Reader。staticReadernullReader()//读取一个字符。intread()//将字符读入数组。intread(char[]cbuf)//将字符读入数组的一部分。abstractintread(char[]cbuf,intoff,intlen)//尝试将字符读入指定的字符缓冲区。intread(CharBuffertarget)//判断这个流是否可以被读取。booleanready()//重置流。voidreset()//跳过字符。longskip(longn)//读取这个reader中的所有字符,并按照读取的顺序将字符写入给定的writer。longtransferTo(Writerout)DataOutputSteam&DataInputStreampublicstaticvoidmain(String[]args){try(OutputStreamout=Files.newOutputStream(Paths.get("C:\\Users\\Xuesong.Bu\\Desktop\\a.txt"))){DataOutputStreamdos=newDataOutputStream(out);dos.writeInt(1);dos.writeLong(2222L);dos.writeDouble(1.23);dos.writeChar('H');dos.writeUTF("hha");}catch(Exceptione){e.printStackTrace();}try(InputStreamin=Files.newInputStream(Paths.get("C:\\Users\\Xuesong.Bu\\Desktop\\a.txt"))){DataInputStreamdis=newDataInputStream(in);inta=dis.readInt();System.out.println(a);长b=dis.readLong();System.out.println(b);双c=dis.readDouble();System.out.println(c);chard=dis.readChar();System.out.println(d);字符串e=dis.readUTF();System.out.println(e);}赶上(例外e){e.printStackTrace();}}//Output122221.23HhhaFiles循环读取文件夹下的子目录和文件Pathpath=Paths.get("/Users/buxuesong/Documents");FileVisitorfileVisitor=newFileVisitor(){@OverridepublicFileVisitResultpreVisitDirectory(Pathdir,BasicFileAttributesattrs)抛出IOException{returnFileVisitResult.CONTINUE;}@OverridepublicFileVisitResultvisitFile(Pathfile,BasicFileAttributesattrs)throwsIOException{fileCount++;返回FileVisitResult.CONTINUE;}@OverridepublicFileVisitResultvisitFileFailed(Pathfile,IOExceptionexc)throwsIOException{returnFileVisitResult.CONTINUE;}@OverridepublicFileVisitResultpostVisitDirectory(Pathdir,IOExceptionexc)throwsIOException{returnFileVisitResult.CONTINUE;}};Files.walkFileTree(path,fileVisitor);System.out.println(fileCount);//其他循环读取所有文件的次数Pathpath=Paths.get("/Users/buxuesong/Documents/python-learn/ch01");Streamstream=Files.walk(path);System.out.println(Files.walk(path).filter(Files::isDirectory).count());System.out.println(Files.walk(path).filter(Files::isRegularFile).count());System.out.println(stream.count());