欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • maven倉庫訪問順序小結(jié)

    maven倉庫訪問順序小結(jié)

    在日常操作中,相信很多人在maven倉庫的優(yōu)先級順序是什么問題上存在疑惑,本文就來介紹一下maven倉庫訪問順序,具有一定的參考價值,感興趣的可以了解一下
    2023-10-10
  • JAVA代碼開發(fā)規(guī)范

    JAVA代碼開發(fā)規(guī)范

    本文主要對JAVA代碼開發(fā)規(guī)范進行詳細介紹,具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • Java實現(xiàn)PDF轉(zhuǎn)HTML/Word/Excel/PPT/PNG的示例代碼

    Java實現(xiàn)PDF轉(zhuǎn)HTML/Word/Excel/PPT/PNG的示例代碼

    這篇文章主要為大家介紹了如何利用Java語言是PDF轉(zhuǎn)HTML、Word、Excel、PPT和PNG功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2022-05-05
  • Springboot中加入druid連接池

    Springboot中加入druid連接池

    這篇文章主要介紹了Springboot中加入druid連接池,Druid是目前最好的數(shù)據(jù)庫連接池。在功能、性能、擴展性方面,都超過其他數(shù)據(jù)庫連接池,同時加入了日志監(jiān)控,下面來看看文章的具體內(nèi)容吧
    2022-01-01
  • 深入XPath的詳解以及Java示例代碼分析

    深入XPath的詳解以及Java示例代碼分析

    本篇文章是對XPath進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • Java中的forEach循環(huán)詳細解讀

    Java中的forEach循環(huán)詳細解讀

    這篇文章主要介紹了Java中的forEach循環(huán)詳細解讀,不要再foreach循環(huán)里面進行元素的add和remove,如果你非要進行remove元素,那么請使用Iterator方式,如果存在并發(fā),那么你一定要選擇加鎖,需要的朋友可以參考下
    2023-12-12
  • 基于springboot微信公眾號開發(fā)(微信自動回復)

    基于springboot微信公眾號開發(fā)(微信自動回復)

    這篇文章主要介紹了基于springboot微信公眾號開發(fā)(微信自動回復),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • spring-data-elasticsearch @Field注解無效的完美解決方案

    spring-data-elasticsearch @Field注解無效的完美解決方案

    這篇文章主要介紹了spring-data-elasticsearch @Field注解無效的完美解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 淺析Spring的事務(wù)實現(xiàn)原理

    淺析Spring的事務(wù)實現(xiàn)原理

    這篇文章主要為大家詳細介紹了Spring中事務(wù)實現(xiàn)的原理,文中的示例代碼講解詳細,對我們學習Spring有一定的幫助,需要的可以參考一下
    2022-11-11
  • Swing常用組件之多行文本區(qū)JTextArea

    Swing常用組件之多行文本區(qū)JTextArea

    這篇文章主要為大家詳細介紹了Swing常用組件之多行文本區(qū)JTextArea,感興趣的朋友可以參考一下
    2016-05-05

最新評論