• 每天学点java_File类


    package com.czj.file;
    
    import java.io.File;
    import java.io.PrintWriter;
    
    
    public class FileDemo {
        public static void main(String[] args) throws Exception {
            File file=new File("test/test.txt");
            file.createNewFile();
            System.out.println("file is exits?"+file.exists());
            
            PrintWriter writer=new PrintWriter(file);
            writer.print("hello file!!");
            writer.println(1);
            writer.print("hello file!!");
            writer.println(2);
            System.out.println(true);
            writer.close();
        }
    
    
    }

    一个简单的File类加输入的实现。

    new File( filename);之后一定要用createNewFile();要不然不会在硬盘创建文件。

     File file=new File("test/test.txt");  这里的test是相对于项目根路径建立的文件夹,是必须的,否则会抛出NullPointerException异常

    PrintWriter   记得一定要关闭。close();
  • 相关阅读:
    hdu
    HUNAN 11567 Escaping (最大流)
    poj -1185 炮兵阵地 (经典状压dp)
    poj
    POJ 2955 Brackets (区间dp)
    csu
    poj
    CSU 1116 Kingdoms
    SPOJ-SQRBR Square Brackets
    退役贴
  • 原文地址:https://www.cnblogs.com/JohnChen-happy/p/4552006.html
Copyright © 2020-2023  润新知