以下内容完全为本人原创,如若转载,请注明出自:http://www.cnblogs.com/XiOrang/

前两天在项目中因为要通过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", "我的梦说别停留等待,就让光芒折射泪湿的瞳孔,映出心中最想拥有的彩虹,带我奔向那片有你的天空,因为你是我的梦 我的梦");
} }

最新文章

  1. java 线程池ThreadPoolExecutor 如何与 AsyncTask() 组合使用。
  2. Discuz! X论坛上传附件到100%自动取消上传的原因及解决方案
  3. [Maven]修改Maven的本地仓库路径
  4. NOI2018准备 Day9
  5. CStringArray用法
  6. php对mysql简单读取的实例
  7. three.js 简介
  8. oracle中查看某个用户名下所有的表以及sequence
  9. python中类的继承
  10. Ubuntu 下配置Samba 实现Linux和windows之间文件共享
  11. HTTP 504 错误
  12. AngularJS进阶(十九)在AngularJS应用中集成百度地图实现定位功能
  13. Implement int sqrt(int x).
  14. python中的深浅copy
  15. P2050 [NOI2012]美食节
  16. ASP.NET AJAX入门系列(6):UpdateProgress控件简单介绍
  17. Java 10 - Java Character类
  18. 关于css中设置属性的常识
  19. 【教程】minicom使用教程
  20. bzoj2982: combination(Lucas定理)

热门文章

  1. JDBC 内部资料 请勿转载 谢谢合作
  2. 我人生中的jQuery选择器
  3. 前端之CSS(一)
  4. Father Christmas flymouse--POJ3160Tarjan
  5. 类型转换bin()、chr()、ord() 、int()、float()、str()、repr()、bytes()、tuple(s )、 list(s )   、unichr(x ) 、 ord(x )  、 hex(x )  、          type()数据类型查询
  6. PHP基础结业感想与总结!
  7. python操作Excel--使用xlrd
  8. bean找不到异常
  9. 由Excel表格导出Latex代码
  10. 关于url中的#-----hash