zoukankan      html  css  js  c++  java
  • java中创建文件并写入的方法

    import java.io.*;
    
    public class CreateFile {
        public static void main(String[] args) {
            String path = "E:\a\s";
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
            String writeFilePath = path + "//newFile.txt";
    //        File writeFile = new File(path, "newFile.txt");
            File writeFile = new File(writeFilePath);
            if (!writeFile.exists()) {
                try {
                    writeFile.createNewFile();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    
            String s = "Print from Java!
    ";
    
            try {
                PrintWriter pw = new PrintWriter(new FileWriter(writeFilePath));
    //            pw.println(s);
                pw.write(s, 5, s.length() - 5);
                pw.print(s);
                pw.print("Here!");
                pw.flush();
                pw.close();
    //            pw.flush();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
    
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(writeFilePath)));
    //            BufferedReader br = new BufferedReader()
                String line = null;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
                br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    
  • 相关阅读:
    snaker数据库表说明
    Oracle 导入、导出DMP(备份)文件
    eclipse debug无法启动
    java----事务
    java----单例模式
    java----spring框架
    mybatis----批量增加与批量删除
    web应用程序状态管理
    JavaWeb----servlet
    HTML / CSS----元素分类
  • 原文地址:https://www.cnblogs.com/liulaolaiu/p/11744368.html
Copyright © 2011-2022 走看看