Java文件操作實例詳解
更新時間:2022年04月25日 17:20:46 作者:寶貝垚
這篇文章主要為大家詳細介紹了Java文件操作實例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Java文件操作的具體代碼,供大家參考,具體內(nèi)容如下
簡介
本程序主要采用了FileInputStream和FileOutputStream兩類對文件進行操作。具體包括通過相對路徑打開文件,三種方法讀取文件,查看文件屬性,追加文件數(shù)據(jù)等。
效果圖:




完整代碼:
package Code.a;
import java.io.*;
public class FileInputStreamDemo {
?? ?
?? ?public static void main(String[] args) {
?? ??? ?//獲取當前目錄;
?? ??? ?File f = new File(".");
?? ??? ?System.out.print("absolute path:"+f.getAbsolutePath()+"\n");
?? ??? ?while(true)
?? ??? ?{
?? ??? ??? ?try {
?? ??? ??? ??? ?//輸入命令;
?? ??? ??? ??? ?System.out.print("Please input your order:");
?? ??? ??? ??? ?BufferedReader stdinBufferedReader;
?? ??? ??? ??? ?String str1 = null;
?? ??? ??? ??? ?stdinBufferedReader = new BufferedReader(new InputStreamReader(System.in));
?? ??? ??? ??? ?str1 = stdinBufferedReader.readLine();
?? ??? ??? ??? ?//相對路徑打開文件;
?? ??? ??? ??? ?File file2 = new File(".\\src\\Code\\a\\Exception.java");
?? ??? ??? ??? ?FileInputStream fis2 = new FileInputStream(file2);
?? ??? ??? ??? ?
?? ??? ??? ??? ?根據(jù)不同的命令,執(zhí)行不同操作;
?? ??? ??? ??? ?//一次性讀取全部數(shù)據(jù)
?? ??? ??? ??? ?if(str1.equals("一次性讀取全部數(shù)據(jù)"))
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?byte[] buf = new byte[(int)(file2.length())];
?? ??? ??? ??? ??? ?fis2.read(buf);
?? ??? ??? ??? ??? ?String str = new String(buf);
?? ??? ??? ??? ??? ?System.out.print(str);
?? ??? ??? ??? ??? ?System.out.print("\n");
?? ??? ??? ??? ?}
?? ??? ??? ??? ?//分塊讀取
?? ??? ??? ??? ?else if(str1.equals("分塊讀取"))
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?int n = 1024,count;
?? ??? ??? ??? ??? ?byte[] buf = new byte[n];
?? ??? ??? ??? ??? ?while((count = fis2.read(buf)) != -1)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?System.out.print(new String(buf,0,count));
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?System.out.print("\n");
?? ??? ??? ??? ?}
?? ??? ??? ??? ?//逐字讀取數(shù)據(jù)
?? ??? ??? ??? ?else if(str1.equals("逐字讀取數(shù)據(jù)"))
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?for(int i = 0; i < file2.length(); i++)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?char ch = (char)(fis2.read());
?? ??? ??? ??? ??? ??? ?System.out.print(ch);
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?System.out.print("\n");
?? ??? ??? ??? ?}
?? ??? ??? ??? ?//退出
?? ??? ??? ??? ?else if(str1.equals("退出"))
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?System.out.print("已退出\n");
?? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?//查看文件屬性
?? ??? ??? ??? ?else if(str1.equals("查看文件屬性"))
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?System.out.print("If the file or catalog exists:"+file2.exists()+"\n");
?? ??? ??? ??? ??? ?System.out.print("If is it a file:"+file2.isFile()+"\n");
?? ??? ??? ??? ??? ?System.out.print("If is it a catalog:"+file2.isDirectory()+"\n");
?? ??? ??? ??? ??? ?System.out.print("FileName:"+file2.getName()+"\n");
?? ??? ??? ??? ??? ?System.out.print("absolute path:"+file2.getAbsolutePath()+"\n");
?? ??? ??? ??? ??? ?System.out.print("The last time that the file was changed:"+file2.lastModified()+"\n");
?? ??? ??? ??? ??? ?System.out.print("The size of the file:"+file2.length()+" bites\n");
?? ??? ??? ??? ?}
?? ??? ??? ??? ?//向文件追加數(shù)據(jù)
?? ??? ??? ??? ?else if(str1.equals("文件追加數(shù)據(jù)"))
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?FileOutputStream fos2 = new FileOutputStream(file2,true);
?? ??? ??? ??? ??? ?System.out.println("Please input the content: ");
?? ??? ??? ??? ??? ?BufferedReader ContentReader;
?? ??? ??? ??? ??? ?String str2 = null;
?? ??? ??? ??? ??? ?ContentReader = new BufferedReader(new InputStreamReader(System.in));
?? ??? ??? ??? ??? ?str2 = ContentReader.readLine();
?? ??? ??? ??? ??? ?fos2.write(str2.getBytes());
?? ??? ??? ??? ??? ?fos2.close();
?? ??? ??? ??? ?}
?? ??? ??? ??? ?//關(guān)閉流對象;
?? ??? ??? ??? ?fis2.close();
?? ??? ??? ?}
?? ??? ??? ?//處理異常;
?? ??? ??? ?catch(FileNotFoundException fnfe) {
?? ??? ??? ??? ?System.out.print("The file open unsuccessfully.");
?? ??? ??? ?}catch(IOException ioe) {
?? ??? ??? ??? ?ioe.printStackTrace();
?? ??? ??? ?}
?? ??? ?}
?? ?}
? ??
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java 實戰(zhàn)練手項目之醫(yī)院預(yù)約掛號系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SpringBoot+Maven+Vue+mysql實現(xiàn)一個醫(yī)院預(yù)約掛號系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11
SpringBoot實現(xiàn)接口統(tǒng)一前綴
本文主要介紹了SpringBoot實現(xiàn)接口統(tǒng)一前綴,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2023-07-07
詳解Spring中@Component和@Configuration的區(qū)別
一直有同學(xué)搞不清Spring中@Component和@Configuration這兩個注解有什么區(qū)別,所以這篇文章小編就給大家簡單介紹一下@Component和@Configuration的區(qū)別,需要的朋友可以參考下2023-07-07
springboot集成RestTemplate及常見的用法說明
這篇文章主要介紹了springboot集成RestTemplate及常見的用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10

