java.io.*

创建文件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
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
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

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 = "庞静赢是王志的娘子\r\n"; 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

更多精彩