创建文件并写入数据
来源:互联网 发布:mac充电先绿灯后变红 编辑:程序博客网 时间:2024/05/17 04:17
public class IO {
//生成文件路径
private static String path = "D:/";
//文件路径+名称
private static String filenameTemp;
/**
* 创建文件
* @param fileName 文件名称
* @param filecontent 文件内容
* @return 是否创建成功,成功则返回true
*/
public static boolean createFile(String fileName,String filecontent){
Boolean bool = false;
filenameTemp = path+fileName+".txt";//文件路径+名称+文件类型
File file = new File(filenameTemp);
try {
//如果文件不存在,则创建新的文件
if(!file.exists()){
file.createNewFile();
bool = true;
System.out.println("success create file,the file is "+filenameTemp);
//创建文件成功后,写入内容到文件里
writeFileContent(filenameTemp, filecontent);
}
} catch (Exception e) {
e.printStackTrace();
}
return bool;
}
/**
* 向文件中写入内容
* @param filepath 文件路径与名称
* @param newstr 写入的内容
* @return
* @throws IOException
*/
public static boolean writeFileContent(String filepath,String newstr) throws IOException{
Boolean bool = false;
String filein = newstr+"\r\n";//新写入的行,换行
String temp = "";
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
PrintWriter pw = null;
try {
File file = new File(filepath);//文件路径(包括文件名称)
//将文件读入输入流
fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
StringBuffer buffer = new StringBuffer();
//文件原有内容
for(int i=0;(temp =br.readLine())!=null;i++){
buffer.append(temp);
// 行与行之间的分隔符 相当于“\n”
buffer = buffer.append(System.getProperty("line.separator"));
}
buffer.append(filein);
fos = new FileOutputStream(file);
pw = new PrintWriter(fos);
pw.write(buffer.toString().toCharArray());
pw.flush();
bool = true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
//不要忘记关闭
if (pw != null) {
pw.close();
}
if (fos != null) {
fos.close();
}
if (br != null) {
br.close();
}
if (isr != null) {
isr.close();
}
if (fis != null) {
fis.close();
}
}
return bool;
}
/**
* 删除文件
* @param fileName 文件名称
* @return
*/
public static boolean delFile(String fileName){
Boolean bool = false;
filenameTemp = path+fileName+".txt";
File file = new File(filenameTemp);
try {
if(file.exists()){
file.delete();
bool = true;
}
} catch (Exception e) {
}
return bool;
}
public static void main(String[] args) {
UUID uuid = UUID.randomUUID();
createFile(uuid+"myfile", "这是我的测试");
}
}
0 0
- 创建文件并写入数据
- MFC创建文件并写入文件数据
- 创建一个csv文件,并写入数据
- 创建xls文件,并写入数据
- c++创建BMP文件并写入数据
- Hadoop 创建文件并写入数据
- PrintWriter 创建文件 并写入数据
- android实现创建文件并写入数据
- VBA|access 创建新的access文件并写入数据
- Android创建文件夹及文件并写入数据
- Android创建文件夹及文件并写入数据
- IO操作 创建一个文件并写入一些文字数据
- java操作excel之创建excel文件,并写入数据
- 创建一个文件,并从键盘写入数据
- Android创建文件夹及文件并写入数据
- Android创建文件夹及文件并写入数据
- 使用java创建netcdf文件,并写入数据
- java利用poi创建excel文件并写入数据
- Android应用启动页闪过短暂白色背景问题
- CTR预估中GBDT与LR融合方案
- could not get batchedbridge, make sure your bundle is packaged correctly
- windows10下利用caffe model训练自己的数据
- c++即时通讯程序
- 创建文件并写入数据
- 微服务架构的优势与不足
- 5.序列&&索引&&同义词
- java-jvm-内存模型
- Terminator终端的配置
- 14.JDBC原理介绍及增删改查示范
- jQuery滚动监听,实现商城楼梯式导航
- linux里mnt下无hgfs文件
- 父类委托机制[1]