• java.io.*


    View Code

    创建文件test2.txt

    public class test {
        public static void main(String[] args) {
            File f = new File("D:\txt\test2.txt");
            if(!f.exists()){
                //可以创建
                try {
                    f.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            System.out.println("文件的位置" + f.getAbsolutePath());
            System.out.println("文件的大小" + f.length());
        }
    }
    View Code

    创建文件夹hello

    import java.io.*;
    
    public class test {
        public static void main(String[] args) {
            File f = new File("D:\txt\hello");
            if (!f.isDirectory()) {
                f.mkdir();
            }
        }
    }
    View Code
    文件字节流实现:文件——>内存  FileInputStream
    View Code

    FileOutputStream   写入文件中

    package io;
    
    /**
     * file类的基本用法
     */
    import java.io.*;
    
    public class test {
        public static void main(String[] args) {
            File f = new File("D:\txt\test1.txt");// 代表文件对象
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(f);
                String s = "庞静赢是王志的娘子
    ";
                String s1 = "王志是庞静赢的相公";
                fos.write(s.getBytes());
                fos.write(s1.getBytes());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    View Code
  • 相关阅读:
    maven插件安装与使用
    java面试题
    关于java的GC
    技术人员要树立自己的品牌
    为什么IT公司都应该鼓励开源
    你应该坚持写博客 即使没有读者
    计算机基础
    收藏 | 产品经理不可不知的 7 种技术思维
    我讨厌你公事公办的样子
    子序列问题【LIS、LCS、LCIS】
  • 原文地址:https://www.cnblogs.com/helloworld2019/p/10831492.html
Copyright © 2020-2023  润新知