当前位置: 移动技术网 > IT编程>开发语言>Java > Java创建文件且写入内容的方法

Java创建文件且写入内容的方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

前两天在项目中因为要通过http请求获取一个比较大的json数据(300kb左右)并且保存,思来想去,最后还是决定将获取到的json数据以文件的形式保存下来,每次使用的时候去读取文件就可以了。

废话不多说了,直接上代码。

以下是代码截图,文章结尾会有完成的代码文件可供下载。

创建文件方法:

写入文件内容方法:

删除文件方法:

测试:

关于文件创建,写入内容,删除。可以根据自己的情况再稍作修改。

以下是代码类。

package com.file.run;
import java.io.bufferedreader;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.printwriter;
import java.util.uuid;
/** 
* @author 夕橘子-o
* @version 2016年7月8日 上午10:38:49 
*/
public class forfile {
//生成文件路径
private static string path = "d:\\file\\";
//文件路径+名称
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) {
// todo: handle exception
}
return bool;
}
public static void main(string[] args) {
uuid uuid = uuid.randomuuid();
createfile(uuid+"myfile", "我的梦说别停留等待,就让光芒折射泪湿的瞳孔,映出心中最想拥有的彩虹,带我奔向那片有你的天空,因为你是我的梦 我的梦");
}
}

以上所述是小编给大家介绍的java创建文件且写入内容的方法,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网