当前位置:首页 » 文件管理 » java获取临时文件夹

java获取临时文件夹

发布时间: 2023-10-13 05:20:49

A. java创建目录或文件夹的方法

1、File类的createNewFile根据抽象路径创建一个新的空文件,当抽象路径制定的文件存在时,创建失败

2、File类的mkdir方法根据抽象路径创建目录

3、File类的mkdirs方法根据抽象路径创建目录,包括创建必需但不存在的父目录

4、File类的createTempFile方法创建临时文件,可以制定临时文件的文件名前缀、后缀及文件所在的目录,如果不指定目录,则存放在系统的临时文件夹下。

5、除mkdirs方法外,以上方法在创建文件和目录时,必须保证目标文件不存在,而且父目录存在,否则会创建失败


示例代码如下:

packagebook.io;

importjava.io.File;
importjava.io.IOException;

publicclassCreateFileUtil{

publicstaticbooleancreateFile(StringdestFileName){
Filefile=newFile(destFileName);
if(file.exists()){
System.out.println("创建单个文件"+destFileName+"失败,目标文件已存在!");
returnfalse;
}
if(destFileName.endsWith(File.separator)){
System.out.println("创建单个文件"+destFileName+"失败,目标文件不能为目录!");
returnfalse;
}
//判断目标文件所在的目录是否存在
if(!file.getParentFile().exists()){
//如果目标文件所在的目录不存在,则创建父目录
System.out.println("目标文件所在目录不存在,准备创建它!");
if(!file.getParentFile().mkdirs()){
System.out.println("创建目标文件所在目录失败!");
returnfalse;
}
}
//创建目标文件
try{
if(file.createNewFile()){
System.out.println("创建单个文件"+destFileName+"成功!");
returntrue;
}else{
System.out.println("创建单个文件"+destFileName+"失败!");
returnfalse;
}
}catch(IOExceptione){
e.printStackTrace();
System.out.println("创建单个文件"+destFileName+"失败!"+e.getMessage());
returnfalse;
}
}


publicstaticbooleancreateDir(StringdestDirName){
Filedir=newFile(destDirName);
if(dir.exists()){
System.out.println("创建目录"+destDirName+"失败,目标目录已经存在");
returnfalse;
}
if(!destDirName.endsWith(File.separator)){
destDirName=destDirName+File.separator;
}
//创建目录
if(dir.mkdirs()){
System.out.println("创建目录"+destDirName+"成功!");
returntrue;
}else{
System.out.println("创建目录"+destDirName+"失败!");
returnfalse;
}
}


(Stringprefix,Stringsuffix,StringdirName){
FiletempFile=null;
if(dirName==null){
try{
//在默认文件夹下创建临时文件
tempFile=File.createTempFile(prefix,suffix);
//返回临时文件的路径
returntempFile.getCanonicalPath();
}catch(IOExceptione){
e.printStackTrace();
System.out.println("创建临时文件失败!"+e.getMessage());
returnnull;
}
}else{
Filedir=newFile(dirName);
//如果临时文件所在目录不存在,首先创建
if(!dir.exists()){
if(!CreateFileUtil.createDir(dirName)){
System.out.println("创建临时文件失败,不能创建临时文件所在的目录!");
returnnull;
}
}
try{
//在指定目录下创建临时文件
tempFile=File.createTempFile(prefix,suffix,dir);
returntempFile.getCanonicalPath();
}catch(IOExceptione){
e.printStackTrace();
System.out.println("创建临时文件失败!"+e.getMessage());
returnnull;
}
}
}

publicstaticvoidmain(String[]args){
//创建目录
StringdirName="D:/work/temp/temp0/temp1";
CreateFileUtil.createDir(dirName);
//创建文件
StringfileName=dirName+"/temp2/tempFile.txt";
CreateFileUtil.createFile(fileName);
//创建临时文件
Stringprefix="temp";
Stringsuffix=".txt";
for(inti=0;i<10;i++){
System.out.println("创建了临时文件:"
+CreateFileUtil.createTempFile(prefix,suffix,dirName));
}
//在默认目录下创建临时文件
for(inti=0;i<10;i++){
System.out.println("在默认目录下创建了临时文件:"
+CreateFileUtil.createTempFile(prefix,suffix,null));
}
}

}

输出结果:


创建目录D:/work/temp/temp0/temp1成功!
目标文件所在目录不存在,准备创建它!
创建单个文件D:/work/temp/temp0/temp1/temp2/tempFile.txt成功!
创建了临时文件:D:work emp emp0 emp1 emp5171.txt
创建了临时文件:D:work emp emp0 emp1 emp5172.txt
创建了临时文件:D:work emp emp0 emp1 emp5173.txt
创建了临时文件:D:work emp emp0 emp1 emp5174.txt
创建了临时文件:D:work emp emp0 emp1 emp5175.txt
创建了临时文件:D:work emp emp0 emp1 emp5176.txt
创建了临时文件:D:work emp emp0 emp1 emp5177.txt
创建了临时文件:D:work emp emp0 emp1 emp5178.txt
创建了临时文件:D:work emp emp0 emp1 emp5179.txt
创建了临时文件:D:work emp emp0 emp1 emp5180.txt
在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5181.txt
在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5182.txt
在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5183.txt
在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5184.txt
在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5185.txt
在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5186.txt
在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5187.txt
在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5188.txt
在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5189.txt
在默认目录下创建了临时文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5190.txt

B. javaweb如何将文件保存到服务器的指定目录

可以把文件目录配置在web.xml文件的初始化参数中, 通过ServletAPI读取文件目录

比如

定义一个Properties文件保存相关配置

#可以上传文件的后缀名

extensions=pptx,docx.doc,txt,jpg,jar

#单个文件的大小1M

fileMaxSize=1048576

#总共上传文件大小5M

totalFileMaxSize=5242880

#文件保存路径

filePath=z:/temp

#临时文件路径

tempDir=z:/temp/temp


使用Listener在服务器启动时加载配置信息

ServletContextcontext=event.getServletContext();
InputStreaminputStream=context
.getResourceAsStream("/WEB-INF/classes/file/upload/commons/uploadConfig.properties");
Propertiesproperties=newProperties();
try{
properties.load(inputStream);
context.setAttribute("fileConfig",properties);
System.out.println("properties="+properties.size());
}catch(IOExceptione){
e.printStackTrace();
}


在你上传文件时通过配置文件读取路径保存

String filePath = ((Properties) this.getServletContext().getAttribute("fileConfig"))

.getProperty(FileUploadConstants.FILE_PATH);

C. java 将服务器内的文件复制

你有FTPClient就比较好办,假如你的两台FTP服务器分别为fs1和fs2
在本地开发代码思路如下:
通过FTPClient连接上fs1,然后下载(可以循环批量下载)到本地服务器,保存到一个临时目录。
下载完成后,FTPClient断开与fs1的连接,记得必须logout。
本地服务器通过FileInputStream将刚下载到临时目录的文件读进来,得到一个List<File>集合。
通过FTPClient连接上fs2,循环List<File>集合,将文件上传至fs2的特定目录,然后清空临时目录,上传完毕后,断开fs2的连接,同样必须logout。

D. java 弹出选择目录框(选择文件夹),获取选择的文件夹路径

java 弹出选择目录框(选择文件夹),获取选择的文件夹路径:
int result = 0;
File file = null;
String path = null;
JFileChooser fileChooser = new JFileChooser();
FileSystemView fsv = FileSystemView.getFileSystemView(); //注意了,这里重要的一句
System.out.println(fsv.getHomeDirectory()); //得到桌面路径
fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
fileChooser.setDialogTitle("请选择要上传的文件...");
fileChooser.setApproveButtonText("确定");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
result = fileChooser.showOpenDialog(chatFrame);
if (JFileChooser.APPROVE_OPTION == result) {
path=fileChooser.getSelectedFile().getPath();
System.out.println("path: "+path);
}

这是另外一种方法得到桌面路径:
File desktop = new File(System.getProperty("user.home")+System.getProperty("file.separator")+"XX");

filechooser.setCurrentDirectory(desktop);

我的文档 路径: fsv.getDefaultDirectory());

user.name 用户的账户名称
user.home 用户的主目录
user.dir 用户的当前工作目录

java.version Java 运行时环境版本
java.vendor Java 运行时环境供应商
java.vendor.url Java 供应商的 URL
java.home Java 安装目录
java.vm.specification.version Java 虚拟机规范版本
java.vm.specification.vendor Java 虚拟机规范供应商
java.vm.specification.name Java 虚拟机规范名称
java.vm.version Java 虚拟机实现版本
java.vm.vendor Java 虚拟机实现供应商
java.vm.name Java 虚拟机实现名称
java.specification.version Java 运行时环境规范版本
java.specification.vendor Java 运行时环境规范供应商
java.specification.name Java 运行时环境规范名称
java.class.version Java 类格式版本号
java.class.path Java 类路径
java.library.path 加载库时搜索的路径列表
java.io.tmpdir 默认的临时文件路径
java.compiler 要使用的 JIT 编译器的名称
java.ext.dirs 一个或多个扩展目录的路径
os.name 操作系统的名称
os.arch 操作系统的架构
os.version 操作系统的版本

E. 如何在Java中创建临时目录/文件夹

File file = new File("目录或者文件名字的路径是绝对的");
file.isFile(
//是文件的话
file.ceateNewFile();
)else{
file.mkDir();
}

F. Java创建文件夹及文件

package xj util;

import java io File;

import java io IOException;

public class CreateFileUtil {

public static boolean CreateFile(String destFileName) {

File file = new File(destFileName);

if (file exists()) {

System out println( 创建单个文件 + destFileName + 失败 目标文件已存在! );

return false;

}

if (destFileName endsWith(File separator)) {

System out println( 创建单个文件 + destFileName + 失败 目标不能是目录! );

return false;

}

if (!file getParentFile() exists()) {

System out println( 目标文件所在路径不存在 准备创建 );

if (!file getParentFile() mkdirs()) {

System out println( 创建目录文件所在的目录失败! );

return false;

}

}

// 创建目标文件

try {

旅族答if (file createNewFile()) {

System out println( 创建单个文件 + destFileName + 成功! );

穗雀return true;

} else {

System out println( 创建单个文件 + destFileName + 失败! );

return false;

}

} catch (IOException e) {

e printStackTrace();

System out println( 创建单个文件 + destFileName + 失败! );

return false;

}

}

public static boolean createDir(String destDirName) {

File dir = new File(destDirName);

if(dir exists()) {

System out println( 创建目录 + destDirName + 失败 目标目录已存在! );

return false;

}

if(!destDirName endsWith(File separator))

destDirName = destDirName + File separator;

// 创建单个目录

if(dir mkdirs()) {

System out println( 创建目录 + destDirName + 成功! );

return true;

} else {

System out println( 创建目录 + destDirName + 成功! );

return false;

}

}

public static String createTempFile(String prefix String suffix String dirName) {

File tempFile = null;

try{

if(dirName == null) {

// 在默认文件夹下创建临时文件

tempFile = File createTempFile(prefix suffix);

return tempFile getCanonicalPath();

}

拆慧else {

File dir = new File(dirName);

// 如果临时文件所在目录不存在 首先创建

if(!dir exists()) {

if(!CreateFileUtil createDir(dirName)){

System out println( 创建临时文件失败 不能创建临时文件所在目录! );

return null;

}

}

tempFile = File createTempFile(prefix suffix dir);

return tempFile getCanonicalPath();

}

} catch(IOException e) {

e printStackTrace();

System out println( 创建临时文件失败 + e getMessage());

return null;

}

}

public static void main(String[] args) {

// 创建目录

String dirName = c:/test/test /test ;

CreateFileUtil createDir(dirName);

// 创建文件

String fileName = dirName + /test /testFile txt ;

CreateFileUtil CreateFile(fileName);

// 创建临时文件

String prefix = temp ;

String suffix = txt ;

for(int i = ; i < ; i++) {

System out println( 创建了临时文件: + CreateFileUtil createTempFile(prefix suffix dirName));

}

}

lishixin/Article/program/Java/hx/201311/25690

热点内容
优质网站为什么用ip服务器 发布:2024-05-17 09:43:34 浏览:792
安卓机图片存在哪里 发布:2024-05-17 09:42:54 浏览:61
ip地址怎么查看服务器上的文件 发布:2024-05-17 09:29:51 浏览:979
轱轮算法 发布:2024-05-17 09:29:10 浏览:95
安卓手机锁屏密码一般怎么画 发布:2024-05-17 09:29:05 浏览:347
堆栈是按组织的存储区域 发布:2024-05-17 09:29:02 浏览:695
sqllinkserver 发布:2024-05-17 09:19:35 浏览:458
访问控制列表的编号 发布:2024-05-17 09:11:24 浏览:785
无线密码忘了怎么办修改密码 发布:2024-05-17 08:59:54 浏览:922
android自定义字体 发布:2024-05-17 08:58:24 浏览:541