java 创建文件与读写
来源:互联网 发布:随心安软件 编辑:程序博客网 时间:2024/06/06 16:59
String rootPath =request.getSession().getServletContext().getRealPath("/");//获取工程根目录(在此基础上可指定子目录)
基本语法如下,注意根据绝对路径来读写文件就好。//创建文件 File file = new File(path); if (!file.exists()) { file.createNewFile();// file.mkdirs(); } //打开文件 PrintStream printStream = new PrintStream(new FileOutputStream(path)); //输入HTML文件内容 stringBuilder.append("<html><head>"); stringBuilder.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"); stringBuilder.append("<title>"); stringBuilder.append("</title>"); stringBuilder.append("</head>"); stringBuilder.append("<body>"); stringBuilder.append("<div>"); stringBuilder.append("hello,hello,ML info!")); stringBuilder.append("</div>"); stringBuilder.append("</body></html>"); //将HTML文件内容写入文件中 printStream.println(stringBuilder.toString());
//新建一个文件夹
publicvoid newFolder(String folderPath) {try {
String filePath = folderPath;
File myFilePath =new File(filePath);
if (!myFilePath.exists()) {
myFilePath.mkdir();
}
} catch (Exception e) {
System.out.println("新建文件夹操作出错");
e.printStackTrace();
}
}
//删除文件夹
publicvoid delFolder(String folderPath){
try{
String filePath = folderPath;
File delPath =new File(filePath);
delPath.delete();
}catch (Exception e) {
System.out.println("删除文件夹操作出错");
e.printStackTrace();
}
}
//新建文件
publicvoid createFile(String fileName){
try{
String myFileName = fileName;
if (!myFileName.exists()) {
myFileName.createNewFile();
}
}catch (Exception e) {
System.out.println("新建文件操作出错");
e.printStackTrace();
}
}
//删除文件
publicvoid delFile(String fileName){
try{
String myFileName = fileName;
myFileName.delete();
}catch (Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace();
}
}
阅读全文
0 0
- Java文件读写与创建
- java 创建文件与读写
- 文件的创建与读写
- C# 文件创建与读写
- java 创建文件夹,读写文件
- java文件创建读写代码
- java与scala文件读写
- Java文件复制与读写
- 内核驱动与文件创建读写
- C# 如何创建与读写txt文件
- C# 如何创建与读写txt文件
- 文件的读写与创建简单例子
- C# 如何创建与读写txt文件
- MFC之文件的创建与读写
- GDAL矢量(.shp)文件读写与创建
- txt文件的创建与读写
- JAVA IO流 创建文件 读写字符串
- Java Android 创建读写TXT文件
- 利用phpExcel实现Excel数据的导入导出(全步骤详细解析)
- Qt 学习之路 2(88):Canvas
- uva 12716 GCD XOR (数论)枚举技巧(筛法) + 思维
- Qt 学习之路 2(89):Canvas(续)
- openlayers4源码解读系列
- java 创建文件与读写
- leaflet1.2.0源码解读系列
- Qt 学习之路 2(90):粒子系统
- 保研经历-从信工所-国防科大-上交-最后确定复旦(信息安全专业)
- 如何让sublime编译和运行c和c++代码
- 方向比努力更重要
- Pandas绘图
- spark(一)
- 关于Windows创建进程的过程