java文件操作輸入輸出結構詳解
更新時間:2022年07月07日 14:47:50 作者:王小王_1
這篇文章主要介紹了java文件操作輸入輸出詳解,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的朋友可以參考一下
一、實驗目的
- 1. 掌握輸入輸出流的總體結構;
- 2. 掌握流的概念;
- 3. 掌握FileInputStream類、FileOutputStream類、FileReader類、FileWriter類的構造方法、常用方法的使用;
- 4. 了解各種流(包括文件流、管道流、連接文件、過濾流、對象的序列化、隨機訪問)的使用。
二、實驗代碼
1.使用Java的輸入輸出
使用Java的輸入、輸出流將一個文本文件的內容按行讀出,每讀出一行就順序添加行號,并寫入到另一個文件中。
package 作業(yè)練習.test4; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; import java.io.File; public class FileScanner { public static void main(String[] args) throws Exception{ System.out.print("請輸入文件名:"); Scanner reader = new Scanner(System.in); String fileName = reader.nextLine(); File f = new File("E:\\Intellij IDEL\\project\\src\\"+fileName); Scanner fi = new Scanner(f); //輸出: String sLine = null; int index = 0; while(fi.hasNext()) { sLine = fi.nextLine(); System.out.println(++index + " " + sLine); try { BufferedWriter out = new BufferedWriter(new FileWriter("test1.txt")); out.write(index + " " + sLine); } catch (IOException e) { } } System.out.println("文件創(chuàng)建成功!"); } }
2.使用RandomAccessFile流將一個文本文件倒置讀出
package 作業(yè)練習.test4; import java.io.*; public class test2 { public static void main(String []args) throws IOException { RandomAccessFile file =new RandomAccessFile("E:\\Intellij IDEL\\project\\src\\test4\\test.txt","r"); long len =file.length(); while(0!=len--) { file.seek(len); char ch =(char)file.read(); System.out.print(ch); } file.close(); } }
3.請分別使用不帶緩沖區(qū)和帶緩沖區(qū)的字節(jié)流復制圖片(或者音頻或者視頻)文件
要求:
- (1) 使用字節(jié)流FileInputStream、FileOutputStream實現(xiàn)復制;
- (2) 在定義字節(jié)緩沖區(qū)大小時,可以嘗試16字節(jié)、256字節(jié)、1024字節(jié)等不同大小的緩沖區(qū)進行復制。
- (3) 請統(tǒng)計采用不同方式進行文件復制所花的時間,并進行分析。
package 作業(yè)練習.test4; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class App14_3 { public static void main(String[] args) { File reader = new File("E:\\Intellij IDEL\\project\\src\\test4\\1.png"); File writer = new File("\\Intellij IDEL\\project\\src\\test4\\2.png"); FileInputStream fis = null; try { fis = new FileInputStream(reader); } catch (FileNotFoundException e1) { e1.printStackTrace(); } BufferedInputStream bis = new BufferedInputStream(fis); FileOutputStream fos = null; try { fos = new FileOutputStream(writer); } catch (FileNotFoundException e) { e.printStackTrace(); } BufferedOutputStream bos = new BufferedOutputStream(fos); byte[] b = new byte[256]; int len = -1; try { while ((len = bis.read(b)) != -1) { bos.write(b, 0, len); bos.flush(); } } catch (IOException e) { e.printStackTrace(); } finally { try { bos.close(); fos.close(); bis.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } } }
package 作業(yè)練習.test4; import java.io.*; public class test3 { public static void main(String []args) throws IOException { // 帶緩沖區(qū)的字節(jié)流拷貝一個文本文件 FileInputStream fin =new FileInputStream("E:\\Intellij IDEL\\project\\src\\test4\\test.txt"); FileOutputStream fout =new FileOutputStream("E:\\Intellij IDEL\\project\\src\\test4\\test1.txt"); byte buf[] =new byte[2014]; //創(chuàng)建字節(jié)數(shù)組,作為臨時緩沖 if(fin.read(buf)!=-1) { fout.write(buf); } System.out.println("文件復制成功"); fin.close(); fout.close(); /*帶緩沖區(qū)的字符流拷貝一個文本文件 FileReader fin =new FileReader("E:\Intellij IDEL\project\src\test4\test2.txt"); BufferedReader din=new BufferedReader(fin) ; FileWriter fou =new FileWriter("E:\Intellij IDEL\project\src\test4\test.txt"); BufferedWriter dou =new BufferedWriter(fou); char c[]=new char[1024]; //創(chuàng)建字符數(shù)組 din.read(c); fou.write(c); System.out.println("文件復制成功"); din.close(); fou.close(); fin.close(); dou.close(); */ } }
4.請分別使用不帶緩沖區(qū)和帶緩沖區(qū)的字符流復制文本文件
要求:
- (1) 使用字符流FileReader、FileWriter實現(xiàn)復制;
- (2) 在定義字符緩沖區(qū)大小時,可以嘗試16字符、256字符、1024字符等不同大小的緩沖區(qū)進行復制。
package 作業(yè)練習.test4; import java.io.*; public class App14_5 { static App14_5 test=new App14_5(); public static String openFile(String fileName){ //打開文件 StringBuffer sb=null; FileInputStream fis=null; try { fis=new FileInputStream(fileName); ; //實例化輸入流對象 byte b[]=new byte[1024]; int len; sb=new StringBuffer(); while( (len = fis.read(b))!=-1 ){ //讀文件并判斷是否到達文件尾 String str=new String(b,0,len); sb.append(str); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ } return sb.toString(); } public static boolean saveFile(String fileName,String content) throws IOException{ boolean state=false; FileOutputStream fos=null; try { fos=new FileOutputStream(fileName); //實例化輸出流對象 //把content寫入到文件中 state=true; } catch (Exception e) { e.printStackTrace(); }finally { } return state; } public static boolean copyFile(String sourceFileName,String destinationFifleName){ boolean sate =false; InputStream fis=null; OutputStream fos=null; try { fis=new FileInputStream(sourceFileName); fos=new FileOutputStream(destinationFifleName); int len; byte buffer[]=new byte[1024]; //此處請?zhí)顚懚嘈? len=fis.read(buffer); String str1=new String(buffer,0,len); byte[] b = str1.getBytes(); fos.write(b); sate =true; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { if(fis!=null) fis.close(); if(fos!=null) fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return sate; } public static void main (String args[]) { App14_5 test=new App14_5(); test.copyFile("E:\\Intellij IDEL\\project\\src\\test4\\test.txt", "E:\\Intellij IDEL\\project\\src\\test4\\test3.txt"); } } }
到此這篇關于java文件操作輸入輸出詳解的文章就介紹到這了,更多相關java文件輸入輸出內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JAVA 生成隨機數(shù)并根據后臺概率靈活生成的實例代碼
本篇文章主要介紹了JAVA 生成隨機數(shù)并根據后臺概率靈活生成的實例代碼,具有一定的參考價值,有興趣的可以了解一下2017-08-08vue+springboot上傳文件、圖片、視頻及回顯到前端詳解
一般來說vue可以使用axios或者fetch等ajax庫發(fā)送文件請求,而springboot則可以使用Spring MVC的方式來處理上傳文件請求,下面這篇文章主要給大家介紹了關于vue+springboot上傳文件、圖片、視頻及回顯到前端的相關資料,需要的朋友可以參考下2023-04-04java eclipse 整個項目或包查找只定字符串并替換操作
這篇文章主要介紹了java eclipse 整個項目或包查找只定字符串并替換操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09