Home  >  Article  >  Java  >  Java implements writing and saving txt files

Java implements writing and saving txt files

王林
王林Original
2020-02-04 17:06:544622browse

Java implements writing and saving txt files

java如何实现写入并保存txt文件?

(推荐学习:java视频教程

实例代码如下:

package TEST;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterDemo {
    public static void main(String[] args) throws IOException {

        write("E:\\1.txt"); //运行主方法
    }
    public static void write(String path)
            throws IOException {
       //将写入转化为流的形式
        BufferedWriter bw = new BufferedWriter(new FileWriter(path));
        //一次写一行
        String ss = "测试数据";
          bw.write(ss);
            bw.newLine();  //换行用

        //关闭流
        bw.close();
        System.out.println("写入成功");
        }


    }

相关文章教程分享:java入门教程

The above is the detailed content of Java implements writing and saving txt files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn