栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

输入/输出(file篇)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

输入/输出(file篇)

(1)Paths接口

    import java.nio.file.Path;
    import java.nio.file.Paths;

    public class Easy {

	    public static void main(String[] args) {				
		    Path path = Paths.get("/home/user/images.txt");//使用get方法创建Path的快捷方式
		    System.out.println(path.getNameCount());//3 返回路径的元素个数
		    System.out.println(path.getName(0));//home getName()方法返回对应下标的元素
		    System.out.println(path.getName(1));//user
		    System.out.println(path.getName(2));//images.txt
		    System.out.println(path.getFileName());//images.txt 返回当前路径获取的文件名
		    System.out.println(path.getFileSystem());//返回当前是什么操作系统
		    System.out.println(path.getParent());// homeuser 返回当前文件的目录的路径
	    }		
    }

(2)java.nio.file.Files类

1.Files.createFile() 方法创建文件

    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.io.IOException;
    import java.nio.file.Files;
    public class Easy {

	    public static void main(String[] args) {				
		    Path newpath = Paths.get("D:\music\emm.txt");//使用get方法创建Path的快捷方式
		    try {//一定要有个捕获异常的程序 否则编译不会通过,并且也保证了安全
		    	Files.createFile(newpath);//在对应路径创建.txt文件
		    } catch (IOException e) {//当捕获到了IOException的异常时进入catch模块
		         // TODO Auto-generated catch block
			    e.printStackTrace();//打印程序出错误的位置以及错误的原因
		    }
	    }		
    }

2. Files.createDirectory() 在对应路径创建文件夹(目录)

	public static void main(String[] args) {				
		Path newpath = Paths.get("D:\music\emq");//使用get方法创建Path的快捷方式
		try {
			Files.createDirectory(newpath);//在对应路径创建文件夹(目录)
		} catch (IOException e) {//捕获Files.createDirectory()方法抛出的异常
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}

3.File.delete() 方法删除对应文件,目录,符号链接

	public static void main(String[] args) {				
		Path newpath = Paths.get("D:\music\emm.txt");//使用get方法创建Path的快捷方式
		try {
			Files.delete(newpath);//输出对应文件
		} catch (IOException e) {//依然是捕获Files.delete();发出的异常
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}

4.Files.deleteIfExists() 删除对应路径文件夹(目录) 前提该目录为空

	public static void main(String[] args) {				
		Path newpath = Paths.get("D:\music\emq");//使用get方法创建Path的快捷方式
		try {
			Files.deleteIfExists(newpath);//删除对应路径文件夹(目录) 前提该目录为空
		} catch (IOException e) {//捕获Files.createDirectory()方法抛出的异常
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}

(3) java.nio.file.DirectoryStream类

DirectoryStream children =  Files.newDirectoryStream(Path类型);              使用该类型检索该目录的子目录,文件,符号链接      

    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.io.IOException;
    import java.nio.file.DirectoryStream;
    import java.nio.file.Files;
    public class Easy {

	    public static void main(String[] args) {				
		    Path newpath = Paths.get("D:\music\emq");//使用get方法创建Path的快捷方式
		    try (DirectoryStream children = 
				    Files.newDirectoryStream(newpath)){//检索当前路径下子目录的所有文件
			    for(Path child : children) {//遍历当前子目录的文件名称
				    System.out.println(child);//打印文件路径和名称
		    	}
			
		    } catch (IOException e) {//捕获Files.createDirectory()方法抛出的异常
			    // TODO Auto-generated catch block
			    e.printStackTrace();
		    }	
	    }		
    }
    打印:
    //D:musicemq新建文件夹
    //D:musicemq新建文本文档 (2).txt
    //D:musicemq新建文本文档 (3).txt
    //D:musicemq新建文本文档.txt

(4)文件的拷贝和移动

使用Files.copy()方法进行拷贝

1.StandardCopyOption.REPLACE_EXISTING 拷贝内容并且生成对应的新文件,若已经 存在文件则覆盖原来的内容                                                       

    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.nio.file.StandardCopyOption;
    import java.io.IOException;
    import java.nio.file.Files;
    public class Easy {

	    public static void main(String[] args) {				
		    Path newpath = Paths.get("D:\music\emq\emm.txt");//使用get方法创建Path的快捷方 
                                                                  //式
		    Path newpath2 = Paths.get("D:\music\emq\fqn.txt");//使用get方法创建Path的快捷 
                                                                 //方式
		    try {
			    Files.copy(newpath, newpath2,
				    StandardCopyOption.REPLACE_EXISTING);//拷贝内容并且生成对应的新文件,若已经 
                                                         //存在文件则覆盖原来的内容
			
	    	} catch (IOException e) {//捕获Files.createDirectory()方法抛出的异常
		    	// TODO Auto-generated catch block
		    	e.printStackTrace();
		    }    	
	    }		
    }

2.File.move() 移动

	public static void main(String[] args) {				
		Path newpath = Paths.get("D:\music\emq\emm.txt");//使用get方法创建Path的快捷方式
		Path newpath2 = Paths.get("D:\music\kee\kee.txt");//使用get方法创建Path的快捷方式
		try {
			Files.move(newpath, newpath2,
				StandardCopyOption.REPLACE_EXISTING);//拷贝文件并且删除源文件
			
		} catch (IOException e) {//捕获Files.createDirectory()方法抛出的异常
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}

(5)文本的写入和读取

1.写入 要有3个条件 文件本身 写入的文本 写入的格式

    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.Arrays;
    import java.util.List;
    import java.io.IOException;
    import java.nio.charset.Charset;
    public class Easy {

    	public static void main(String[] args) {				
		    Path newpath = Paths.get("D:\music\emq\emm.txt");//使用get方法创建Path的快 
                                                                //捷方式
		    Charset charset = Charset.forName("US-ASCII");//选择写入格式
		    String line1 = "Hello World";
		    String line2 = "easy";
		    List lines = Arrays.asList(line1,line2);//将写入的String放入集合
		    try {
			    Files.write(newpath, lines, charset);//在对应文件中使用对应格式写入对应文本
			
		    } catch (IOException e) {//捕获Files.createDirectory()方法抛出的异常
			    // TODO Auto-generated catch block
			    e.printStackTrace();
		    }	
	    }		
    }

2.读取 读取的集合 读取的格式

	public static void main(String[] args) {				
		Path newpath = Paths.get("D:\music\emq\emm.txt");//使用get方法创建Path的快捷方式
		List myList = null;//初始文件接收的集合
		Charset  charset = Charset.forName("US-ASCII");//读取格式
		try {
			myList = Files.readAllLines(newpath,charset);//将文本以列的方式分割,然后返回文本的 
                                                         //集合
			
		} catch (IOException e) {//捕获Files.createDirectory()方法抛出的异常
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		for(String list : myList) {
			
			System.out.println(list);//遍历文本
		}
	}
    //Hello World
    //easy

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/584595.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号