Java文件操作實(shí)例詳解
本文實(shí)例為大家分享了Java文件操作的具體代碼,供大家參考,具體內(nèi)容如下
簡(jiǎn)介
本程序主要采用了FileInputStream和FileOutputStream兩類對(duì)文件進(jìn)行操作。具體包括通過(guò)相對(duì)路徑打開(kāi)文件,三種方法讀取文件,查看文件屬性,追加文件數(shù)據(jù)等。
效果圖:




完整代碼:
package Code.a;
import java.io.*;
public class FileInputStreamDemo {
?? ?
?? ?public static void main(String[] args) {
?? ??? ?//獲取當(dāng)前目錄;
?? ??? ?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();
?? ??? ??? ??? ?//相對(duì)路徑打開(kāi)文件;
?? ??? ??? ??? ?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)閉流對(duì)象;
?? ??? ??? ??? ?fis2.close();
?? ??? ??? ?}
?? ??? ??? ?//處理異常;
?? ??? ??? ?catch(FileNotFoundException fnfe) {
?? ??? ??? ??? ?System.out.print("The file open unsuccessfully.");
?? ??? ??? ?}catch(IOException ioe) {
?? ??? ??? ??? ?ioe.printStackTrace();
?? ??? ??? ?}
?? ??? ?}
?? ?}
? ??
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
maven倉(cāng)庫(kù)訪問(wèn)順序小結(jié)
在日常操作中,相信很多人在maven倉(cāng)庫(kù)的優(yōu)先級(jí)順序是什么問(wèn)題上存在疑惑,本文就來(lái)介紹一下maven倉(cāng)庫(kù)訪問(wèn)順序,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
Java實(shí)現(xiàn)PDF轉(zhuǎn)HTML/Word/Excel/PPT/PNG的示例代碼
這篇文章主要為大家介紹了如何利用Java語(yǔ)言是PDF轉(zhuǎn)HTML、Word、Excel、PPT和PNG功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-05-05
基于springboot微信公眾號(hào)開(kāi)發(fā)(微信自動(dòng)回復(fù))
這篇文章主要介紹了基于springboot微信公眾號(hào)開(kāi)發(fā)(微信自動(dòng)回復(fù)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
spring-data-elasticsearch @Field注解無(wú)效的完美解決方案
這篇文章主要介紹了spring-data-elasticsearch @Field注解無(wú)效的完美解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
淺析Spring的事務(wù)實(shí)現(xiàn)原理
這篇文章主要為大家詳細(xì)介紹了Spring中事務(wù)實(shí)現(xiàn)的原理,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Spring有一定的幫助,需要的可以參考一下2022-11-11

