如何创建一个文件,但当文件已存在时返回出错信息? 那你可以先判断是否存在 boolean exists() Tests whether the file denoted by this abstract pathname exists. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先根据你的文件路径定义一个文件对象File Myfile = new File("d:\myfile.txt");if( Myfile.exists()){//文件存在 }else{//文件不存在} if(文件不存在时) 创建文件;如果在这样两个函数调用之间别的进程创建了这个文件,就会被它删除。希望这两步操作能是原子的,不会被打断。在Windows 下的CreateFile(...)就有个标志,叫CREATE_NEW,可以规定在要创建的文件已存在时不要删除它。java里不知道有没有相应的方法。 Java中对文件的操作 java中提供了io类库,可以轻松的用java实现对文件的各种操作。下面就来说一下如何用java来实现这些操作。 1。新建目录<%@ page contentType="text/html;charset=gb2312"%><%String filePath="c:/aaa/";filePath=filePath.toString();//中文转换java.io.File myFilePath=new java.io.File(filePath);if(!myFilePath.exists())myFilePath.mkdir();%> 2。新建文件<%@ page contentType="text/html;charset=gb2312"%><%@ page import="java.io.*" %><%String filePath="c:/哈哈.txt";filePath=filePath.toString();File myFilePath=new File(filePath);if(!myFilePath.exists())myFilePath.createNewFile();FileWriter resultFile=new FileWriter(myFilePath);PrintWriter myFile=new PrintWriter(resultFile);String strContent = "中文测试".toString();myFile.println(strContent);resultFile.close();%> 怎么在程序中加一个按钮? 奇怪的styleClass??? jTabbedPane 加入对话框 数据库系统打包问题 如何JTextArea中改变部分字体的颜色,急!!!! read和write的问题 分享一段代码,顺便问个问题 如何判断一个Integer对象中的数据是半角数字??? java深度抓取的问题 帮忙看一下JSP调用EJB的错误 udp在发送中文数据包(英文没有影响)只接收到一半,是什么原因怎么解决呀 各位大侠们,给我一个提示吧!!!在线等
File Myfile = new File("d:\myfile.txt");
if( Myfile.exists()){
//文件存在
}
else{
//文件不存在}
创建文件;如果在这样两个函数调用之间别的进程创建了这个文件,就会被它删除。
希望这两步操作能是原子的,不会被打断。在Windows 下的CreateFile(...)就有个标志,叫CREATE_NEW,可以规定在要创建的文件已
存在时不要删除它。java里不知道有没有相应的方法。
<%
String filePath="c:/aaa/";
filePath=filePath.toString();//中文转换
java.io.File myFilePath=new java.io.File(filePath);
if(!myFilePath.exists())
myFilePath.mkdir();
%> 2。新建文件<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*" %>
<%
String filePath="c:/哈哈.txt";
filePath=filePath.toString();
File myFilePath=new File(filePath);
if(!myFilePath.exists())
myFilePath.createNewFile();
FileWriter resultFile=new FileWriter(myFilePath);
PrintWriter myFile=new PrintWriter(resultFile);
String strContent = "中文测试".toString();
myFile.println(strContent);
resultFile.close();
%>