Java實現讀寫文件功能的代碼分享

下面是利用Java實現讀寫文件功能的示例代碼

讀文件

TextRead.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
 
public class TextRead {
        /**
         * 讀取txt文件的內容
         * @param file 想要讀取的文件對象
         * @return 返回文件內容
         */
        public static String txt2String(File file){
            StringBuilder result = new StringBuilder();
 
            try{
                BufferedReader br = new BufferedReader(new FileReader(file));//構造一個BufferedReader類來讀取文件
                String s = null;
                while((s = br.readLine())!=null){//使用readLine方法,一次讀一行
                    result.append(System.lineSeparator()+s);
                }
                br.close();
            }catch(Exception e){
                e.printStackTrace();
            }
            System.out.println("TextRead" + result.toString());
            return result.toString();
        }
 
        public static void main(String[] args){
            File file = new File("D:\\fileCreate\\2022_08_17_10_08_501.txt");
            System.out.println(txt2String(file));
        }
    }

寫文件

WriteFile.java

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class WriteFile {
 
    public static void writeFileContent(String path, String MyStrs){
 
 
        FileWriter fw=null;
        //文件路徑
        String filePath = path;
        //日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy_MM_dd_HH_MM_SS");
        SimpleDateFormat dfTime = new SimpleDateFormat("yyyy-MM-dd:HH:MM:SS  ");
        String fileName=df.format(new Date())+".txt";
        File newFile=new File(filePath);
        if(!newFile.exists()) {
            newFile.mkdir();
        }
        File f=new File(filePath,fileName);
        try {
            //創建文件
            f.createNewFile();
            fw=new FileWriter(f);
            //寫入數據
            String poem = MyStrs;
//            System.out.println("WriteFile" + poem);
 
            fw.write(dfTime.format(new Date())+ poem);
        } catch (
                IOException e) {
            throw new RuntimeException("文件創建失敗");
        }finally {
            try {
                fw.close();
            } catch (IOException e) {
                throw new RuntimeException("文件流關閉失敗");
            }
        }
    }
 
 
 
    public static void main(String[] strings)
    {
 
        String filePath="D:\\fileCreate";
        String strs = "西北有高樓,上與浮雲齊;" +
                "煙籠寒水月籠沙,夜泊秦淮近酒傢;" +
                "商女不知亡國恨,隔江猶唱後庭花。" +
                "Hello world" +
                "1234567890";
 
        WriteFile.writeFileContent(filePath, strs);
        System.out.println("WriteFile" + strs);
 
    }
}

主函數

Main.java

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class Main {
    public static void main(String[] args) {
 
        String filePath="D:\\fileCreate";
        String strs = "西北有高樓,上與浮雲齊;" +
                "煙籠寒水月籠沙,夜泊秦淮近酒傢;" +
                "商女不知亡國恨,隔江猶唱後庭花。" +
                "Hello world" +
                "12345667890";
 
        WriteFile.writeFileContent(filePath, strs);
 
        File file = new File("D:\\fileCreate\\2022_08_17_10_08_501.txt");
 
//        String showFile = new String();
//        showFile = TextRead.txt2String(file);
//        System.out.printf(showFile);
//        System.out.println(showFile);
 
        System.out.println("file = " + TextRead.txt2String(file));
 
        System.out.println(TextRead.txt2String(file));
 
 
    }
}

實現效果

不知道為什麼,writefile運行就出錯瞭

到此這篇關於Java實現讀寫文件功能的代碼分享的文章就介紹到這瞭,更多相關Java讀寫文件內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: