java File类的基本使用方法总结
作者:lijiao 时间:2022-05-10 15:15:36
Java IO中File的使用是比较频繁的,在文件的上传和删除中都会用到的。比如我们在写管理系统的时候有可能会用到图片的上传,和删除。那么我们就会用到Java的 File来处理。
Java中File的基本使用创建和删除文件:
public class FileDemo {
public static void main(String[] args) {
File f=new File("d:"+File.separator+"io.txt");
//File.separator 得到“\”
//File.pathSeparator得到是“;”
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//等等一段时间,可以查看文件的生成
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(f.exists()){
f.delete();
}else{
System.out.println("文件不存在");
}
}
}
Java File示例使用:在J2EE开发中使用的图片上传功能代码:
public void fileUpload(@RequestParam MultipartFile[] myfiles,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
String imgPath = "/uploads" + "/";
File directory = new File(request.getSession().getServletContext()
.getRealPath("/")
+ imgPath);
String desFileName = null;
String fileNewName = null;
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
String originalFilename = null;
for (MultipartFile myfile : myfiles) {
if (myfile.isEmpty()) {
out.write("请选择文件后上传");
out.flush();
} else {
originalFilename = myfile.getOriginalFilename();
if (null != originalFilename && originalFilename.length() > 0) {
fileNewName = UUID.randomUUID() + originalFilename;
desFileName = directory.toString() + "/" + fileNewName;
}
try {
FileUtils.copyInputStreamToFile(myfile.getInputStream(),
new File(desFileName));
} catch (IOException e) {
e.printStackTrace();
out.write("文件上传失败,请重试!!");
out.flush();
}
}
}
out.print(fileNewName);
out.flush();
}
并且其中文件夹生成的代码如下:
File f1=new File("d:"+File.separator+"test");
f1.mkdir();
//获取文件夹名称的方法
f1.getName();
这是Java IO中的基础使用,也是使用比较频繁的部分。
标签:java,File类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c# 实现自动扫雷
2021-09-01 09:25:58
Spring Boot启动banner定制的步骤详解
2023-03-04 19:30:20
![](https://img.aspxhome.com/file/2023/6/97776_0s.png)
java程序设计语言的优势及特点
2022-08-16 09:12:57
java图的深度优先遍历实现随机生成迷宫
2023-06-26 06:06:05
![](https://img.aspxhome.com/file/2023/2/62672_0s.jpg)
详解Spring整合Ehcache管理缓存
2022-02-10 00:50:17
.Net多进程通信共享内存映射文件Memory Mapped
2022-04-30 23:33:57
![](https://img.aspxhome.com/file/2023/6/72456_0s.jpg)
java实现列表、集合与数组之间转化的方法
2023-11-29 10:17:38
springboot2.0使用Hikari连接池的方法(替换druid)
2023-04-12 00:54:33
![](https://img.aspxhome.com/file/2023/1/70531_0s.png)
Kotlin使用静态变量与静态方法详解
2021-10-11 07:03:40
通过Mybatis实现单表内一对多的数据展示示例代码
2021-12-20 12:34:31
![](https://img.aspxhome.com/file/2023/1/64011_0s.png)
AndroidStudio:手势识别
2022-04-04 00:53:58
![](https://img.aspxhome.com/file/2023/1/137261_0s.jpg)
Android 图片缩放与旋转的实现详解
2023-03-10 10:57:41
![](https://img.aspxhome.com/file/2023/4/138994_0s.jpg)
一起来了解Java的File类和IO流
2022-04-12 09:23:15
![](https://img.aspxhome.com/file/2023/9/74969_0s.png)
关于java中构造函数的一些知识详解
2023-11-27 22:09:11
c# 成员类型访问权限低于字段本身的实现
2021-12-23 07:08:27
![](https://img.aspxhome.com/file/2023/9/78129_0s.png)
Android实现授权访问网页的方法
2022-05-13 15:39:57
Java动态添加view的方法
2023-06-11 04:20:38
C#语言中字符类char的使用方法(总结)
2022-09-04 17:25:43
Android侧滑效果简单实现代码
2023-02-25 09:50:00
![](https://img.aspxhome.com/file/2023/8/138448_0s.gif)
详解C语言内核字符串拷贝与比较
2023-11-02 15:22:30
![](https://img.aspxhome.com/file/2023/2/107392_0s.png)