java的io操作(将字符串写入到txt文件中)

时间:2022-05-28 22:59:23 


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;

public class WriteStringToTxt {

    public void WriteStringToFile(String filePath) {
        try {
            File file = new File(filePath);
            PrintStream ps = new PrintStream(new FileOutputStream(file));
            ps.println("https://www.jb51.net");// 往文件里写入字符串
            ps.append("https://www.jb51.net");// 在已有的基础上添加字符串
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void WriteStringToFile2(String filePath) {
        try {
            FileWriter fw = new FileWriter(filePath, true);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.append("在已有的基础上添加字符串");
            bw.write("abc\r\n ");// 往已有的文件上添加字符串
            bw.write("def\r\n ");
            bw.write("hijk ");
            bw.close();
            fw.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void WriteStringToFile3(String filePath) {
        try {
            PrintWriter pw = new PrintWriter(new FileWriter(filePath));
            pw.println("abc ");
            pw.println("def ");
            pw.println("hef ");
            pw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void WriteStringToFile4(String filePath) {
        try {
            RandomAccessFile rf = new RandomAccessFile(filePath, "rw");
            rf.writeBytes("op\r\n");
            rf.writeBytes("app\r\n");
            rf.writeBytes("hijklllll");
            rf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void WriteStringToFile5(String filePath) {
        try {
            FileOutputStream fos = new FileOutputStream(filePath);
            String s = "https://www.jb51.netl";
            fos.write(s.getBytes());
            fos.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String filePath = "E:\\link.txt";
        // new WriteStringToTxt().WriteStringToFile(filePath);
        // new WriteStringToTxt().WriteStringToFile2(filePath);
        // new WriteStringToTxt().WriteStringToFile3(filePath);
        // new WriteStringToTxt().WriteStringToFile4(filePath);
        new WriteStringToTxt().WriteStringToFile5(filePath);
    }
}

标签:java,io
0
投稿

猜你喜欢

  • IDEA插件EasyCode及MyBatis最优配置步骤详解

    2023-11-09 03:19:19
  • 支持SpEL表达式的自定义日志注解@SysLog介绍

    2023-08-27 09:38:42
  • RocketMQ消息生产者是如何选择Broker示例详解

    2023-11-10 21:45:49
  • Spring Cloud项目前后端分离跨域的操作

    2022-05-20 08:11:16
  • java设计模式笔记之适配器模式

    2021-12-27 05:27:30
  • Windows下RabbitMQ安装及配置详解

    2022-04-29 08:11:18
  • Java DWR内存泄漏问题解决方案

    2022-02-28 02:35:07
  • Java面向对象程序设计:类的定义,静态变量,成员变量,构造函数,封装与私有,this概念与用法详解

    2022-07-12 03:35:14
  • java中匿名内部类详解

    2022-10-06 14:56:56
  • Java开发实现人机猜拳游戏

    2023-10-17 16:01:35
  • SpringBoot实现PPT格式文件上传并在线预览功能

    2023-07-22 14:44:56
  • IDEA集成MyBatis Generator插件的使用

    2023-08-12 00:28:47
  • Java实现年兽大作战游戏详解

    2023-11-08 04:28:05
  • Java 回调callback举例详解

    2023-11-11 16:25:09
  • springboot ErrorPageFilter的实际应用详解

    2023-11-24 01:02:59
  • Java中this和super的区别及this能否调用到父类使用

    2023-01-05 12:03:13
  • Java数据结构之链表相关知识总结

    2023-11-02 00:29:28
  • springboot使用自定义注解实现aop切面日志

    2023-11-11 09:14:48
  • Java实现单链表反转的多种方法总结

    2023-11-11 02:28:08
  • Java读取TXT文件内容的方法

    2023-11-23 22:33:41
  • asp之家 软件编程 m.aspxhome.com