当前位置:首页 » 编程语言 » java文件存在

java文件存在

发布时间: 2023-06-10 17:06:33

java判断文件是否存在不存在就创建

用File类中的.exists()方法判断是否存在
mkdirs创建目录
createNewFile()创建文件
多看看API文档

boolean
exists()

测试此抽象路径名表示的文件或目录是否存在。
createNewFile()

当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
boolean
mkdirs()

创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。

❷ 怎么把指定文件存在指定目录下java

java写入文件到指定文件夹的方法主要有两种:利用PrintStream和利用StringBuffer
例如将文本“I'm the text to be write”写入到文件夹D:/test下,并命名为test.txt,则两种方式简单实现代码如下:
1. 利用PrintStream写文件
public void PrintStreamDemo(){
try {
FileOutputStream out=new FileOutputStream("D:/test.txt");
PrintStream p=new PrintStream(out);
p.println("I'm the text to be write");
} catch (FileNotFoundException e){
e.printStackTrace();
}
}
2. 利用StringBuffer写文件

public void StringBufferDemo() throws IOException{

File file=new File("D:/test.txt");
if(!file.exists())
file.createNewFile();
FileOutputStream out=new FileOutputStream(file,true);
StringBuffer sb=new StringBuffer();
sb.append("I'm the text to be write");
out.write(sb.toString().getBytes("utf-8"));
}
out.close();
}

提示:利用StringBuffer写文件可以设定使用何种编码,有效解决中文问题。

❸ java怎么判断一个文件是否存在

用java.io.File类的public boolean exists()方法就能判断,如:
import java.io.*;
public class Demo
{
public static void main(String[] args) throws Exception
{
//将p指定为文件的路径
String p="test.txt";
File f=new File(p);
if(f.isFile())
{
if(f.exists())
{
System.out.println("文件"+p+"存在。");
}
else
{
System.out.println("文件"+p+"不存在。");
}
}
else
{
System.out.println(p+"不是文件。");
}
}
}

❹ Java中怎样根据文件的路径去判断该文件夹中是否存在该文件

1.File testFile = new File(testFilePath);
if(!testFile .exists()){
testFile.mkdirs();
System.out.println("测试文件夹不存在");
}

2.File testFile = new File(testFilePath);
if(!testFile .exists()){
testFile.createNewFile();
System.out.println("测试文件不存在");
}
java中File类自带一个检测方法exists可以判断文件或文件夹是否存在,一般与mkdirs方法(该方法相较于mkdir可以创建包括父级路径,推荐使用该方法)或者createNewFile方法合作使用。
1,如果路径不存在,就创建该路径

2,如果文件不存在,就新建该文件

❺ java 根据文件名判断文件是否存在

File类自带判断文件(或者路径)是否存在的方法。举个例子:

Stringpath="D:\";
Stringfilename="test.txt";
Filefile=newFile(path+filename);
if(file.exists()){
System.out.println("文件"+filename+"存在");
}else{
System.out.println("文件"+filename+"不存在")
}

❻ Java判断文件目录以及文件是否存在

近日在项目中遇到一个问题,需要在指定的目录下面,找到指定的文件。查找相关方法后,特将方法写出来以便日后用到。 public static File checkExist(String filepath)throwsException{File file=newFile(filepath);if(file.exists()) {//判断文件目录的存在System. out .println("文件夹存在!");if(file.isDirectory()){//判断文件的存在性System. out .println("文件存在!");}else{ file.createNewFile();//创建文件System. out .println("文件不存在,创建文件成功!");}}else{System. out .println("文件夹不存在!"); File file2=newFile(file.getParent()); file2.mkdirs();System. out .println("创建文件夹成功!");if(file.isDirectory()){System. out .println("文件存在!");}else{ file.createNewFile();//创建文件System. .println("文件不存在,创建文件成功!");}}returnfile;}

❼ Java建立文件如果文件存在则不建立

你在创建之前可以加个判断,判断文件是否存在,如果存在直接return或者你路径文件名加1

热点内容
python元素替换list 发布:2025-02-13 20:03:48 浏览:772
windows系统账户名和密码是多少 发布:2025-02-13 20:03:02 浏览:530
我的世界带有商店服务器好吗 发布:2025-02-13 20:02:50 浏览:615
东莞加密软件 发布:2025-02-13 20:02:05 浏览:869
可以玩游戏的云服务器 发布:2025-02-13 19:55:35 浏览:303
php授权系统 发布:2025-02-13 19:55:22 浏览:415
php截取字符乱码 发布:2025-02-13 19:53:54 浏览:89
安卓什么时候限制app权限 发布:2025-02-13 19:53:12 浏览:110
红米手机录音文件夹 发布:2025-02-13 19:41:33 浏览:236
android适配屏幕 发布:2025-02-13 19:40:30 浏览:793