• Java读和写文件


    TextRead.java

    1. import java.io.BufferedReader;
    2. import java.io.File;
    3. import java.io.FileReader;
    4. public class TextRead {
    5. /**
    6. * 读取txt文件的内容
    7. * @param file 想要读取的文件对象
    8. * @return 返回文件内容
    9. */
    10. public static String txt2String(File file){
    11. StringBuilder result = new StringBuilder();
    12. try{
    13. BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
    14. String s = null;
    15. while((s = br.readLine())!=null){//使用readLine方法,一次读一行
    16. result.append(System.lineSeparator()+s);
    17. }
    18. br.close();
    19. }catch(Exception e){
    20. e.printStackTrace();
    21. }
    22. System.out.println("TextRead" + result.toString());
    23. return result.toString();
    24. }
    25. public static void main(String[] args){
    26. File file = new File("D:\\fileCreate\\2022_08_17_10_08_501.txt");
    27. System.out.println(txt2String(file));
    28. }
    29. }

    WriteFile.java

    1. import java.io.File;
    2. import java.io.FileWriter;
    3. import java.io.IOException;
    4. import java.text.SimpleDateFormat;
    5. import java.util.Date;
    6. public class WriteFile {
    7. public static void writeFileContent(String path, String MyStrs){
    8. FileWriter fw=null;
    9. //文件路径
    10. String filePath = path;
    11. //日期格式
    12. SimpleDateFormat df = new SimpleDateFormat("yyyy_MM_dd_HH_MM_SS");
    13. SimpleDateFormat dfTime = new SimpleDateFormat("yyyy-MM-dd:HH:MM:SS ");
    14. String fileName=df.format(new Date())+".txt";
    15. File newFile=new File(filePath);
    16. if(!newFile.exists()) {
    17. newFile.mkdir();
    18. }
    19. File f=new File(filePath,fileName);
    20. try {
    21. //创建文件
    22. f.createNewFile();
    23. fw=new FileWriter(f);
    24. //写入数据
    25. String poem = MyStrs;
    26. // System.out.println("WriteFile" + poem);
    27. fw.write(dfTime.format(new Date())+ poem);
    28. } catch (
    29. IOException e) {
    30. throw new RuntimeException("文件创建失败");
    31. }finally {
    32. try {
    33. fw.close();
    34. } catch (IOException e) {
    35. throw new RuntimeException("文件流关闭失败");
    36. }
    37. }
    38. }
    39. public static void main(String[] strings)
    40. {
    41. String filePath="D:\\fileCreate";
    42. String strs = "西北有高楼,上与浮云齐;" +
    43. "烟笼寒水月笼沙,夜泊秦淮近酒家;" +
    44. "商女不知亡国恨,隔江犹唱后庭花。" +
    45. "Hello world" +
    46. "1234567890";
    47. WriteFile.writeFileContent(filePath, strs);
    48. System.out.println("WriteFile" + strs);
    49. }
    50. }

    主函数  Main.java

    1. import java.io.File;
    2. import java.io.FileWriter;
    3. import java.io.IOException;
    4. import java.text.SimpleDateFormat;
    5. import java.util.Date;
    6. public class Main {
    7. public static void main(String[] args) {
    8. String filePath="D:\\fileCreate";
    9. String strs = "西北有高楼,上与浮云齐;" +
    10. "烟笼寒水月笼沙,夜泊秦淮近酒家;" +
    11. "商女不知亡国恨,隔江犹唱后庭花。" +
    12. "Hello world" +
    13. "12345667890";
    14. WriteFile.writeFileContent(filePath, strs);
    15. File file = new File("D:\\fileCreate\\2022_08_17_10_08_501.txt");
    16. // String showFile = new String();
    17. // showFile = TextRead.txt2String(file);
    18. // System.out.printf(showFile);
    19. // System.out.println(showFile);
    20. System.out.println("file = " + TextRead.txt2String(file));
    21. System.out.println(TextRead.txt2String(file));
    22. }
    23. }

     

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

     

     

  • 相关阅读:
    利用gpu加速神经网络算法,为什么用gpu 模型训练
    mingw-gcc编译窗体和console
    传统Spring项目的创建和使用xml文件来保存对象和取对象
    机器学习-数值特征
    基于springboot的社区医院管理系统的设计
    idea中的debug界面上没有进入方法的红色按钮
    使用ant-design-vue实现换肤功能
    一文读懂,WMS仓储管理系统与ERP有什么区别
    谷歌(edge)浏览器过滤,只查看后端发送的请求
    天宇优配|GDR海外发行热情高 资本市场互联互通提速
  • 原文地址:https://blog.csdn.net/moonlightpeng/article/details/126381516