软件编程
位置:首页>> 软件编程>> java编程>> Java实现读写文件功能的代码分享

Java实现读写文件功能的代码分享

作者:天人合一peng  发布时间:2023-02-07 04:07:02 

标签: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));

}
}

实现效果

Java实现读写文件功能的代码分享

Java实现读写文件功能的代码分享

不知道为什么,writefile运行就出错了

Java实现读写文件功能的代码分享

来源:https://blog.csdn.net/moonlightpeng/article/details/126381516

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com