java创建和读取文件
来源:互联网 发布:单片机图形化编程 源码 编辑:程序博客网 时间:2024/06/05 19:12
package io;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
public class IOHandle {
public static void main(String[] args) {
String path = "d:/新建文件夹/中国.txt";
String content = "中国/t北京/t天安门";
createFileTwo(path, content);
readFileOne(path);
}
/**
* 创建文件并向文件中写入内容
*
* @param path
* @param content
* @return
*/
public static boolean createFileOne(String path, String content) {
try {
File file = new File(path);
if (file.isDirectory()) {
if (!file.exists()) {
file.mkdirs();
}
} else {
String p = file.getParent();
File mkdir = new File(p);
mkdir.mkdirs();
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(path, false);
out.write(content.getBytes("utf-8"));
out.flush();
out.close();
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
return false;
}
return true;
}
/**
*
* @param path
* @param Str
* @return
*/
public static boolean createFileTwo(String path, String Str) {
File file = new File(path);
try {
if (file.isDirectory()) {
if (!file.exists()) {
file.mkdirs();
}
} else {
String p = file.getParent();
File mkdir = new File(p);
mkdir.mkdirs();
file.createNewFile();
}
FileWriter writer = new FileWriter(file,true);
BufferedWriter bwriter = new BufferedWriter(writer);
bwriter.write(Str);
bwriter.flush();
bwriter.close();
} catch (IOException e) {
return false;
}
return true;
}
/**
* 读取文件内容
*
* @param path
*/
public static void readFileOne(String path) {
File file = new File(path);
if (file.exists()) {
try {
FileInputStream in = new FileInputStream(file);
byte buffer[] = new byte[1024];
int c;
while ((c = in.read(buffer)) != -1) {
String ss = new String(buffer,0,c);
System.out.println(ss);
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("文件不存在");
}
}
public static void copyFile(String src, String dest) {
try {
FileInputStream in = new FileInputStream(src);
File file = new File(dest);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
int c;
byte buffer[] = new byte[1024];
while ((c = in.read(buffer)) != -1) {
for (int i = 0; i < c; i++) {
out.write(buffer[i]);
}
}
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除特定目录下所有文件夹和文件
*
* @param path
*/
public static void delDir(String path) {
File file = new File(path);
File files[] = file.listFiles();
for (int i = 0; i < files.length; i++) {
File f = files[i];
if (f.isDirectory()) {
delDir(f.getAbsolutePath());
} else {
f.delete();
}
}
}
/**
* 输出特定目录下所有文件夹和文件名
*
* @param path
*/
public static void selectDir(String path) {
File file = new File(path);
File files[] = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
System.out.println(files[i].getParent());
selectDir(files[i].getAbsolutePath());
} else {
System.out.println(files[i].getName());
}
}
}
}
- java创建和读取文件
- 利用java创建和读取excel文件
- 简单用Java Excel 创建和读取 Excel文件
- JAVA读取文件的创建时间和修改时间
- JAVA读取文件的创建时间和修改时间
- JAVA读取文件的创建时间和修改时间
- Java读取文件创建时间和最后修改时间
- JAVA读取文件的创建时间和修改时间
- java 读取文件的创建 时间和修改时间
- Java读取文件创建时间和最后修改时间
- JAVA中创建ZIP文件,并向ZIP文件中写入并读取条目和数据
- 用JAVA创建,读取XML文件
- 用JAVA创建,读取XML文件
- 用JAVA创建,读取XML文件
- 用JAVA创建,读取XML文件
- java 创建文件 写入内容 读取内容
- xml文件的创建和读取
- 创建和读取内存映像文件
- 实现删除前的提示框
- Web上传文件在线压缩的实现
- Linux环境下用c语言写的播放wav文件的小程序
- mysql 在命令行下的如何取消命令
- 复合数据类型--此处偶做了index_by表的试验
- java创建和读取文件
- Javascript Performance Test (3)
- 算法与笨办法对比实验
- vb.net禁用关闭红叉
- 关于asp.net中forms身份验证
- aspxasp开始测试自己的seo成果啦
- 绑定变量的分析理解
- Microsoft SQL Server Database Publishing Wizard 使用
- javascript 回车提交