java io操作_Java 的IO操作(文件的读,写操作)
/* ?* FileEditor.java ?* ?* Created on 2006年9月13日, 下午2:22 ?* ?* To change this template, choose Tools | Template Manager ?* and open the template in the editor. ?*/ /** *這是第一個類,沒有利用緩沖技術 */ package fileeditor; import java.io.*; /** ?* ?* @author Administrator ?*/ public class FileEditor { ??? ??? /** Creates a new instance of FileEditor */ ??? public FileEditor() { ??? } ???? /*用于創建文件; ???? */ ??? public static void createFile(String fileName) throws IOException{ ??????????? String file=fileName; ??????????? FileWriter f=new FileWriter(file); ??????????? f.close(); ???? } ??? /*用于更新文件內容; ???? */ ??? public static void updateFileContent(String fileName, byte[] content) throws IOException{ ??????? ByteArrayOutputStream f=new ByteArrayOutputStream(); ??????? byte[] buf=content; ??????? f.write(buf); ??????? OutputStream out=new FileOutputStream(fileName); ??????? f.writeTo(out); ??????? f.close(); ??????? out.close(); ??? } ??? /*用于獲取文件內容(返回比特流(字節)數組) ???? */ ??? public static byte[] getFileContent(String fileName) throws IOException { ??????? int size; ??????? File f=new File(fileName); ??????? FileInputStream in = null; ??????? in = new FileInputStream(f); ??????? size=in.available(); ??????? byte[] content =new byte[size]; ??????? in.read(content,0,size); ??????? in.close(); ??????? return content; ??? } ??? /*用于刪除文件; ???? */ ??? public static void deleteFile(String fileName) throws? IOException{ ??????? String file=fileName; ??????? File f=new File(fileName); ??????? f.delete(); ??? } } /** *這里是第二個類,用了緩沖技術 */ /*用于更新文件內容; ???? */ ??? public static void updateFileContent(String fileName, byte[] content) throws IOException{ ??????? byte[] buf=content; ??????? BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(fileName)); ??????? out.write(content); ??????? out.close(); ??? } ??? /*用于創建文件; ???? */ ??? public static void createFile(String fileName)throws IOException{ ??????? ??????????? String file=fileName; ??????????? FileWriter f=new FileWriter(file); ??????????? f.close(); ????? ??? } ??? /*用于獲取文件內容 ???? */ ??? public static byte[] getFileContent(String fileName) throws IOException { ??????? File f=new File(fileName); ??????? BufferedInputStream in =? new BufferedInputStream(new FileInputStream(f)); ??????? byte[] content =null; ??????? in.read(content);???????? ??????? in.close(); ??????? return content; ??? } ??? /*用于刪除文件; ???? */ ??? public static void deleteFile(String fileName) throws? IOException{ ??????? String file=fileName; ??????? File f=new File(fileName); ??????? f.delete(); ??? } /** *這是主函數 */ public static void main(String[] args) { ??????? byte[] content = null; ?????? // BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); ??????? String fromFile = null; ??????? String toFile=null; ??????? try { ???????? fromFile="g://asd.jpg"; ???????? content=FileEditor.getFileContent(fromFile); ???????? for (int i=0;i<1000;i++){ ???????????? toFile="g://test//"; ???????????? toFile+=i; ???????????? toFile=".jpg"; ???????????? FileEditor.createFile(toFile);//創建目標文件; ???????????? FileEditor.updateFileContent(toFile,content);????? ???????? } ???????? ??????? } catch (IOException ex) { ??????????? ex.printStackTrace(); ??????? } ??? } ?在這次試驗中,我想對比用緩沖流和不用緩沖流讀寫文件的效率,同一個文件(150k)復制1000份,在我機器上,不用緩沖的是4秒,用了也是4秒,所以沒比較出來。后來當我試圖復制更大的文件(30M)時出現了錯誤。 提示 :Exception in thread "main" java.lang.OutOfMemoryError: Java heap space : 我調試了半天,也沒弄懂,希望高手指教。其他的問題沒什么,這個類完全可以適用與小于30M的任何情況下使用。
總結
以上是生活随笔為你收集整理的java io操作_Java 的IO操作(文件的读,写操作)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ntopng mysql_网络流量监控工
- 下一篇: 豌豆粉的功效与作用、禁忌和食用方法