首页 / JAVA / Java IO流的介绍&基本使用
Java IO流的介绍&基本使用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java IO流的介绍&基本使用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6136字,纯文字阅读大概需要9分钟。
内容图文
![Java IO流的介绍&基本使用](/upload/InfoBanner/zyjiaocheng/628/8390dac704bd42d1b799cbd4c81e5e70.jpg)
IO流的基本介绍使用
* @author: Connor-郭 * @description: File类 名称、路径 * getName() 名称 * getPath() 相对、绝对 * getAbsolutePath() 绝对 * getParent() 上层路径,没有返回null * * @date: 2020年4月2日 * @time: 下午1:37:12 */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class IOTest01 { @Test public void testA() { File file = new File("IO_study01/gtaf2.jpg"); //基本操作 System.out.println("名称"+file.getName()); System.out.println("路径"+file.getPath()); System.out.println("绝对路径"+file.getAbsolutePath()); System.out.println("父路径"+file.getParent()); System.out.println("父对象"+file.getParentFile()); }
文件的基本使用
* @author: Connor-郭 * @description: File类 名称、路径 * getName() 名称 * getPath() 相对、绝对 * getAbsolutePath() 绝对 * getParent() 上层路径,没有返回null * * @date: 2020年4月2日 * @time: 下午1:37:12 */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class IOTest01 { @Test public void testA() { File file = new File("IO_study01/gtaf2.jpg"); //基本操作 System.out.println("名称"+file.getName()); System.out.println("路径"+file.getPath()); System.out.println("绝对路径"+file.getAbsolutePath()); System.out.println("父路径"+file.getParent()); System.out.println("父对象"+file.getParentFile()); }
* @author: Connor-郭 * @description: File类 其他信息 * * length():文件的字节数,文件不存在返回0 * * createNewFile():指定文件存在,返回false * delete():删除已经存在的文件 * * @date: 2020年4月2日 * @time: 下午1:37:12 */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class IOTest03 { @Test public void testA() { File file2 = new File("D:/software/JavaWebTools/SpringEclipse-workspace/IO_study01/s.jpg"); System.out.println(file2.length()); } @Test public void testB() throws IOException { File file = new File("D:/software/JavaWebTools/SpringEclipse-workspace/IO_study01/io.txt"); boolean b = file.createNewFile(); System.out.println(b); } }
文件夹操作
/** * * @author: Connor-郭 * @description: 创建目录 * 1.mkdir():上级目录不存在。无法创建 * 2.mkdirs():上级目录不存在一同创建 * * * @date: 2020年4月2日 * @time: 下午1:56:47 */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class DirDemo01 { @Test public void testA() { // File file = new File("D:/software/JavaWebTools/SpringEclipse-workspace/IO_study01/dir/test"); // // //创建目录 mkdirs // boolean mkdirs = file.mkdirs(); // System.out.println(mkdirs); //创建目录 mkdir File file1 = new File("D:/software/JavaWebTools/SpringEclipse-workspace/IO_study01/dir/test2"); boolean mkdir = file1.mkdir(); System.out.println(mkdir); } }
文件夹递归操作
* @author: Connor-郭 * @description: * 列出下一级 * 1.list() 列出下级名称 * 2.listFiles():列出下级File对象 * * @date: 2020年4月2日 * @time: 下午1:56:47 */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class DirDemo02 { public static void main(String[] args) { File file1 = new File("D:/software/JavaWebTools/SpringEclipse-workspace/IO_study01"); pringName(file1, 0); } /** * 递归打印 文件夹子孙目录 * * @param file * @param deep */ public static void pringName(File file, int deep) { for (int i = 0; i < deep; i++) { System.out.print("-"); //分隔符 } if (null == file || !file.exists()) {//判断文件是否存在 return; } System.out.println(file.getName());//打印文件名 if (file.isDirectory()) {//判断是否是文件夹 for (File s : file.listFiles()) {//文件夹继续遍历 递归 pringName(s, deep + 1);//递归调用 } } } @Test public void testA() { File file1 = new File("D:/software/JavaWebTools/SpringEclipse-workspace/IO_study01"); // 列出下级名称 String[] list = file1.list(); for (String string : list) { System.out.println(string); } // 列出下级File对象 File[] listFiles = file1.listFiles(); for (File file : listFiles) { System.out.println(file.getAbsolutePath()); } } }
面向对象练习,获取文件大小
/** * * @author: Connor-郭 * @description: 使用面向对象 统计 文件大小. * * * @date: 2020年4月2日 * @time: 下午1:56:47 */ public class DirDemo03Count { // 大小 private long len; // 文件夹 private String dirPath; // 源文件 private File src; // 文件数量 private int fileCount; // 文件夹数量 private int DirCount = -1; // 根文件夹 子孙级包含自身文件保存自身 public DirDemo03Count(String path) { this.dirPath = path; this.src = new File(path); count(this.src); } // 获取 文件大小 public void count(File file) { // 判断文件非空,存在 if (null != file && file.exists()) { // 判断是否文件 if (file.isFile()) { // 文件自增 this.fileCount++; // 加长度 this.len += file.length(); } else { // 文件夹自增 this.DirCount++; // 继续遍历 子孙级文件 for (File f : file.listFiles()) { // 递归调用 count(f); } } } } public long getLen() { return len; } public Integer getFileCount() { return fileCount; } public Integer getDirCount() { return DirCount; } public static void main(String[] args) { DirDemo03Count dirDemo03Count = new DirDemo03Count( "D:/software/JavaWebTools/SpringEclipse-workspace/IO_study01"); System.out.println("文件大小 " + dirDemo03Count.getLen()); System.out.println("文件数量" + dirDemo03Count.getFileCount()); System.out.println("文件夹数量 " + dirDemo03Count.getDirCount()); } }
文件编码
** * 从字节到 字符 编码操作 * 从字符到 字节 解码操作 * * @author: Connor-郭 * @description: * * @date: 2020年4月2日 * @time: 下午5:00:26 */ public class ContentEncode { public static void main(String[] args) throws Exception { decode(); } //编码 字节到 字符 public static void encode() throws Exception { String str = "郭宗鑫a"; //编码: 字节数组 byte[] bytes = str.getBytes(); //默认使用工程字符集 UTF-8 中文2字节,英文1字节 System.out.println(bytes.length); bytes = str.getBytes("UTF-16LE");//默认1个字符为2个字节 System.out.println(bytes.length); bytes = str.getBytes("GBK");//默认 中文2字节,英文1字节 System.out.println(bytes.length); } //解码 字符到 字节 public static void decode() throws Exception { String str = "郭宗鑫a"; //编码: 字节数组 byte[] bytes = str.getBytes(); //默认使用工程字符集 UTF-8 中文2字节,英文1字节 //解码 字符 str = new String(bytes,"UTF-8"); System.out.println(str); str = new String(bytes,"UTF-16LE"); System.out.println(str); str = new String(bytes,"GBK"); System.out.println(str); } }
个人记录,勿喷勿水
内容总结
以上是互联网集市为您收集整理的Java IO流的介绍&基本使用全部内容,希望文章能够帮你解决Java IO流的介绍&基本使用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。