日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

IO流数据读写总结

發布時間:2023/11/27 生活经验 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IO流数据读写总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.用java自帶的IO讀寫方法 官方API網站:http://docs.oracle.com/javase/7/docs/api/

2.Apache的Commons-io-2.4.jar中的方法,參考文檔:http://commons.apache.org/proper/commons-io/javadocs/api-2.4/index.html

/*    * 用apache的ommons-io-2.4.jar的ileUtils類中的方法讀寫數據* 常用方法請參考幫助文檔* */public  void readWriteCopyFile(){File file1 = new File("c:/to.txt");File file2 = new File("c:/to_new.txt");try {String input = FileUtils.readFileToString(file1,"gb2312");System.out.println(input);FileUtils.copyFile(file1, file2);} catch (IOException e) {// TODO Auto-generated catch block
            e.printStackTrace();}String fileName = "C:/to.txt";File file = new File(fileName);String fileContent = "";fileContent +="Helloworld";try {FileUtils.writeStringToFile(file, fileContent, "gb2312");} catch (IOException e) {e.printStackTrace();}}/*    * 用apache的ommons-io-2.4.jar的ileUtils類中的方法讀寫數據* */public  void URLToFileTest(){try {URL url = new URL("http://www.163.com");  File file = new File("c:\\163.html");  FileUtils.copyURLToFile(url, file);} catch (MalformedURLException e) {// TODO Auto-generated catch block
            e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch block
            e.printStackTrace();}  }  /*    * 用apache的ommons-io-2.4.jar的ileUtils類中的方法讀寫url內容數據* */public  void readURL() throws IOException{InputStream in = new URL( "http://www.blogjava.net/ashutc/archive/2010/07/13/325933.html" ).openStream();try {System.out.println( IOUtils.toString( in ) );} finally {IOUtils.closeQuietly(in);}}/*    * 用apache的ommons-io-2.4.jar的ileUtils類中的方法讀寫url內容數據* */public  void downTolocal(){try {InputStream in = new URL("http://img4.cache.netease.com/tech/2015/12/10/201512101531442cb6f_550.png").openStream();byte [] gif = IOUtils.toByteArray(in);//IOUtils.write(gif,new FileOutputStream(new File("c:/test.gif")));    //將字符串內容直接寫到文件中FileUtils.writeByteArrayToFile(new File("c:/test.png"),gif) ;            //將字節數組內容寫到文件中System.out.println("done");IOUtils.closeQuietly(in);} catch (MalformedURLException e) {// TODO Auto-generated catch block
            e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch block
            e.printStackTrace();}}/** 使用java自帶的帶有緩存區字節讀寫數據---提高讀寫速度*  */public void RwByByteStream() {File fileIn = new File("D:/java.docx");File fileOut = new File("D:/java_new.docx");long before = System.currentTimeMillis();FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream(fileIn);fos = new FileOutputStream(fileOut);} catch (FileNotFoundException e1) {// TODO Auto-generated catch block
                e1.printStackTrace();}BufferedInputStream bis = new BufferedInputStream(fis,100000);        //設置緩沖區及大小BufferedOutputStream bos = new BufferedOutputStream(fos,100000);    //設置輸出流緩沖區及大小byte [] buffer = new byte[10000];int l;int num = 0;try {while((l=bis.read(buffer))!=-1){bos.write(buffer, 0, l);num++;}System.out.println(System.currentTimeMillis()-before+"ms");System.out.println(num);} catch (IOException e) {// TODO Auto-generated catch block
                e.printStackTrace();}finally{try {bos.close();fos.close();bis.close();fis.close();} catch (IOException e) {// TODO Auto-generated catch block
                    e.printStackTrace();}}}/** 使用java字符流讀寫數據* * */public void RWByCharStream() {try {//File file = new File("d:/java.txt");FileInputStream fis = new FileInputStream("d:/java.txt");FileOutputStream fos = new FileOutputStream("d:/java_new.txt",true);InputStreamReader isr = new InputStreamReader(fis, "gb2312");OutputStreamWriter osw = new OutputStreamWriter(fos, "gb2312");char input[] = new char[100];int l = 0;while ((l = isr.read(input)) != -1) {//String inputString = new String(input,0,l);osw.write(input,0,l);}isr.close();fis.close();osw.close();fos.close();System.out.println("done");} catch (FileNotFoundException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}    /** 使用java緩沖區字符流讀寫數據* * */public void RWByBufferedCharStream() {try {//File file = new File("d:/java.txt");FileInputStream fis = new FileInputStream("d:/java.txt");FileOutputStream fos = new FileOutputStream("d:/java_new.txt",true);    //加true表示追加寫到這個文件中,否則覆蓋此文件    InputStreamReader isr = new InputStreamReader(fis, "gb2312");OutputStreamWriter osw = new OutputStreamWriter(fos, "gb2312");BufferedReader br = new BufferedReader(isr);    //設置緩沖區BufferedWriter bw = new BufferedWriter(osw);
//                PrintWriter pw = new PrintWriter(osw);
                String input;while ((input = br.readLine()) != null) {bw.write(input);bw.newLine();System.out.println(input);
//                    pw.println(input);
                }br.close();        //各流后打開的先關閉,先打開后關閉
                bw.flush();bw.close();
//                pw.close();
                isr.close();fis.close();osw.close();fos.close();System.out.println("done");} catch (FileNotFoundException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}

?

轉載于:https://www.cnblogs.com/gongjian/p/6130955.html

總結

以上是生活随笔為你收集整理的IO流数据读写总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。