BufferedOutputStream bufferedOutputStream = null; try { //创建流 src文件如果不存在,程序会帮忙创建 bufferedOutputStream = FileUtil.getOutputStream("C:\\Users\\Administrator\\Desktop\\xuzhu\\getOutputStreamTest1.txt"); String str = "getOutputStreamTest内容1 \ngetOutputStreamTest内容2"; byte[] sb = str.getBytes(); bufferedOutputStream.write(sb); bufferedOutputStream.flush(); } catch (IOException e) { //抛出一个运行时异常(直接停止掉程序) throw new RuntimeException("运行时异常",e); }finally { IoUtil.close(bufferedOutputStream); }
BufferedWriter bufferedWriter = null; try { //是否追加 Boolean isAppend = false; //创建流 bufferedWriter = FileUtil.getWriter("C:\\Users\\Administrator\\Desktop\\xuzhu\\getWriterTest.txt",CharsetUtil.UTF_8,isAppend); String str = "getWriterTest1 \ngetWriterTest2"; bufferedWriter.write(str); bufferedWriter.flush(); } catch (IOException e) { //抛出一个运行时异常(直接停止掉程序) throw new RuntimeException("运行时异常",e); }finally { IoUtil.close(bufferedWriter); }
BufferedWriter bufferedWriter = null; try { //是否追加 Boolean isAppend = true; //创建流 bufferedWriter = FileUtil.getWriter("C:\\Users\\Administrator\\Desktop\\xuzhu\\getWriterTest.txt",CharsetUtil.CHARSET_UTF_8,isAppend); String str = "getWriterTest1 \ngetWriterTest2"; bufferedWriter.write(str); bufferedWriter.flush(); } catch (IOException e) { //抛出一个运行时异常(直接停止掉程序) throw new RuntimeException("运行时异常",e); }finally { IoUtil.close(bufferedWriter); }
PrintWriter printWriter = null; try { //是否追加 Boolean isAppend = false; //创建流 printWriter = FileUtil.getPrintWriter("C:\\Users\\Administrator\\Desktop\\xuzhu\\getPrintWriterTest1.txt",CharsetUtil.UTF_8,isAppend); printWriter.write("小虚竹"); printWriter.append(" 你真帅~"); //写入时换行 printWriter.println("我稀罕你"); printWriter.write("我爱你"); printWriter.flush(); } catch (IORuntimeException e) { //抛出一个运行时异常(直接停止掉程序) throw new RuntimeException("运行时异常",e); }finally { IoUtil.close(printWriter); }
参考案例:
PrintWriter printWriter = null; try { //是否追加 Boolean isAppend = false; //创建流 printWriter = FileUtil.getPrintWriter("C:\\Users\\Administrator\\Desktop\\xuzhu\\getPrintWriterTest1.txt",CharsetUtil.CHARSET_UTF_8,isAppend); printWriter.write("小虚竹"); printWriter.append(" 你真帅~"); //写入时换行 printWriter.println("我稀罕你"); printWriter.write("我爱你"); printWriter.flush(); } catch (IORuntimeException e) { //抛出一个运行时异常(直接停止掉程序) throw new RuntimeException("运行时异常",e); }finally { IoUtil.close(printWriter); }