Java文件实例
2021/8/25 17:06:18
本文主要是介绍Java文件实例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
文件创建
File createTempFile(String prefix, String suffix,File directory) prefix文件名 suffix文件后缀 directory 文件目录
File file = null; File dir = new File("C:\\Users\\MACHENIKE\\Desktop"); file = File.createTempFile ("createText", ".txt", dir); System.out.println(file.getPath());
源码 分析可知当文件名长度小于3时会抛出异常
public static File createTempFile(String prefix, String suffix, File directory) throws IOException { if (prefix.length() < 3) throw new IllegalArgumentException("Prefix string too short"); if (suffix == null) suffix = ".tmp"; File tmpdir = (directory != null) ? directory : TempDirectory.location(); SecurityManager sm = System.getSecurityManager(); File f; do { f = TempDirectory.generateFile(prefix, suffix, tmpdir); if (sm != null) { try { sm.checkWrite(f.getPath()); } catch (SecurityException se) { // don't reveal temporary directory location if (directory == null) throw new SecurityException("Unable to create temporary file"); throw se; } } } while ((fs.getBooleanAttributes(f) & FileSystem.BA_EXISTS) != 0); if (!fs.createFileExclusively(f.getPath())) throw new IOException("Unable to create temporary file"); return f; }
文件写入实例
在路径下有create.txt文件则修改,没有就创建
@Test public void file() throws IOException { BufferedWriter out = new BufferedWriter(new FileWriter("C:\\Users\\MACHENIKE\\Desktop\\create.txt")); out.write("hello word"); out.close(); System.out.println("文件创建成功!"); }
读取文件内容
BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\MACHENIKE\\Desktop\\createText.txt")); String str; while ((str = in.readLine()) != null) { System.out.println(json); }
上面读取文件可能会乱码,需要在读取的时候转换成相应的编码
//一、转换成byte数组 BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\MACHENIKE\\Desktop\\createText.txt")); String str; while ((str = in.readLine()) != null) { byte[] bytes = str.getBytes(); String json = new String(bytes, "UTF-8"); System.out.println(json); } //二、读取时候设置编码 BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\MACHENIKE\\Desktop\\createText.txt"),"UTF-8")); String str; while ((str = in.readLine()) != null) { System.out.println(str); }
删除文件
File file = new File("C:\\Users\\MACHENIKE\\Desktop\\createText.txt"); if(file.delete()){ System.out.println(file.getName() + " 删除成功!"); }else{ System.out.println("删除失败!"); }
检测文件是否存在
File file = new File("C:\\Users\\MACHENIKE\\Desktop\\nonentity.txt"); System.out.println(file.exists());
修改文件名称
File oldName = new File("C:\\Users\\MACHENIKE\\Desktop\\createText.txt");//旧文件 File newName = new File("C:\\Users\\MACHENIKE\\Desktop\\newText.txt");//新文件 if (oldName.renameTo(newName)){ System.out.println("成功"); } else { System.out.println("失败"); }
获取文件大小
File file = new File("C:\\Users\\MACHENIKE\\Desktop\\newText.txt"); if (!file.exists() || !file.isFile()) { System.out.println("文件不存在"); } System.out.println(file.length());//单位字节
设置文件只读
File file = new File("C:\\Users\\MACHENIKE\\Desktop\\newText.txt"); System.out.println(file.setReadOnly()); //该方法返回true,如果应用程序写入文件,否则该方法返回false。 System.out.println(file.canWrite());
这篇关于Java文件实例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-28MQ底层原理资料详解:新手入门教程
- 2024-11-28MQ项目开发资料详解:新手入门教程
- 2024-11-28MQ项目开发资料详解:入门与初级用户指南
- 2024-11-28MQ消息队列资料入门教程
- 2024-11-28MQ消息队列资料:新手入门详解
- 2024-11-28MQ消息中间件资料详解与应用教程
- 2024-11-28MQ消息中间件资料入门教程
- 2024-11-28MQ源码资料详解与入门教程
- 2024-11-28MQ源码资料入门教程
- 2024-11-28RocketMQ底层原理资料详解