代码如下:

 package com.wiscom.utils;

 import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException; public class FileUtil { /**
* 以字节为单位读取文件,通常用于读取二进制文件,如图片
* @param path
* @return
*/
public static String readByBytes(String path) {
String content = null; try {
InputStream inputStream = new FileInputStream(path);
StringBuffer sb = new StringBuffer();
int c = 0;
byte[] bytes = new byte[1024];
/*
* InputStream.read(byte[] b)
*
* Reads some number of bytes from the input stream and stores them into the buffer array b. 从输入流中读取一些字节存入缓冲数组b中
* The number of bytes actually read is returned as an integer. 返回实际读到的字节数
* This method blocks until input data is available, end of file is detected, or an exception is thrown.
* 该方法会一直阻塞,直到输入数据可以得到、或检测到文件结束、或抛出异常 -- 意思是得到数据就返回
*/
while ((c = inputStream.read(bytes)) != -1) {
sb.append(new String(bytes, 0, c, "utf-8"));
} content = sb.toString();
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} return content;
} /**
* 以行为单位读取文件,常用于读取面向行的格式化文件
* @param path
* @return
*/
public static String readByLines(String path) {
String content = null; try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "utf-8")); StringBuffer sb = new StringBuffer();
String temp = null;
while ((temp = bufferedReader.readLine()) != null) {
sb.append(temp);
} content = sb.toString();
bufferedReader.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} return content;
} /**
* 以字符为单位读取文件,常用于读取文本文件
* @param path
* @return
*/
public static String readByChars(String path) {
String content = null; try { Reader reader = new InputStreamReader(new FileInputStream(path), "utf-8");
StringBuffer sb = new StringBuffer(); char[] tempchars = new char[1024];
while (reader.read(tempchars) != -1) {
sb.append(tempchars);
} content = sb.toString();
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
return content;
} /**
* 把内容content写的path文件中
* @param content
* @param path
* @return
*/
public static boolean saveAs(String content, String path) { FileWriter fw = null; //System.out.println("把内容:" + content + ", 写入文件:" + path); try {
/**
* Constructs a FileWriter object given a File object.
* If the second argument is true, then bytes will be written to the end of the file rather than the beginning.
* 根据给定的File对象构造一个FileWriter对象。 如果append参数为true, 则字节将被写入到文件的末尾(向文件中追加内容)
*
* Parameters:
* file, a File object to write to 带写入的文件对象
* append, if true, then bytes will be written to the end of the file rather than the beginning
* Throws:
* IOException -
* if the file exists but is a directory rather than a regular file,
* does not exist but cannot be created,
* or cannot be opened for any other reason
* 报异常的3种情况:
* file对象是一个存在的目录(不是一个常规文件)
* file对象是一个不存在的常规文件,但不能被创建
* file对象是一个存在的常规文件,但不能被打开
*
*/
fw = new FileWriter(new File(path), false);
if (content != null) {
fw.write(content);
}
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
if (fw != null) {
try {
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
}

java 文件读写工具 FileUtil的更多相关文章

  1. java文件读写工具类

    依赖jar:commons-io.jar 1.写文件 // by FileUtilsList<String> lines = FileUtils.readLines(file, " ...

  2. java 文件读写的有用工具

    java 文件读写的有用工具 package org.rui.io.util; import java.io.BufferedReader; import java.io.File; import j ...

  3. java文件处理工具类

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedRead ...

  4. properties文件读写工具类

    java代码: import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; ...

  5. Spring-Boot ☞ ShapeFile文件读写工具类+接口调用

    一.项目目录结构树 二.项目启动 三.往指定的shp文件里写内容 (1) json数据[Post] { "name":"test", "path&qu ...

  6. android 文件读写工具类

    将可以序列化的对象通过base64编码后进行保存 但是感觉多数情况下,不需要采用这个功能,直接保存原始的json字符串,取出来之后再进行解析即可 package com.wotlab.home.mon ...

  7. list集合、txt文件对比的工具类和文件读写工具类

    工作上经常会遇到处理大数据的问题,下面两个工具类,是在处理大数据时编写的:推荐的是使用map的方式处理两个list数据,如果遇到list相当大数据这个方法就起到了作用,当时处理了两个十万级的list, ...

  8. java文件读写的两种方式

    今天搞了下java文件的读写,自己也总结了一下,但是不全,只有两种方式,先直接看代码: public static void main(String[] args) throws IOExceptio ...

  9. java文件读写操作

    Java IO系统里读写文件使用Reader和Writer两个抽象类,Reader中read()和close()方法都是抽象方法.Writer中 write(),flush()和close()方法为抽 ...

随机推荐

  1. 2016年中国独角兽企业估值榜 TOP300

    2016年中国独角兽企业估值榜 TOP300[完整榜单] 类型:品牌资讯/名企动态 阅读:6735次 来源: 中商情报网 我要评论   摘要:独角兽公司是什么?独角兽公司指的是那些估值达到10亿美元以 ...

  2. 支付宝 python alipay 集成(转)

    即时到帐只是支付宝众多商家服务中的一个,表示客户付款,客户用支付宝付款,支付宝收到款项后,马上通知你,并且此笔款项与交易脱离关系,商家可以马上使用. 即时到帐只对企业客户服务,注册成功企业账号以后,申 ...

  3. Matlab 稀疏矩阵函数

    eye 单位矩阵zeros 全零矩阵ones 全1矩阵rand 均匀分布随机阵genmarkov 生成随机Markov矩阵linspace 线性等分向量logspace 对数等分向量logm 矩阵对数 ...

  4. DOM,jquery,vue

    DOM 部分引用自引用自七色花的姐姐 1.DOM全称 Document Object Model,即文档对象模型,它允许脚本(js)控制Web页面.窗口和文档 2.DOM的作用 做网页的都知道,想要做 ...

  5. Chrome浏览器一直请求clients1.google.com:443

    浏览器莫名其妙地发一大堆请求,往clients1.google.com:443,把各种扩展各种插件关了都不管用,后来才发现问题,取消“密码和表单”中的“自动填充”功能,即可解决.

  6. PHP来控制Linux,ssh2来控制服务器端

    注意:我们用PHP来控制Linux,php环境可以在windows也可以在linux,但是我们要控制的机器是一台linux(被控制的linux关闭selinux和firewalld). 如果php在l ...

  7. mysql自定义function 写递归查询子节点

    #存储文本信息表 CREATE TABLE WordInfoEntity( word_id ) PRIMARY KEY NOT NULL, # 主键ID UUID word_greda :正文文本 , ...

  8. Directx11教程(12) 禁止alt+enter全屏窗口

    原文:Directx11教程(12) 禁止alt+enter全屏窗口        在D3D11应用程序中,我们按下alt+enter键,会切换到全屏模式.有时候,我们在WM_SIZE中有一些代码,全 ...

  9. 支付宝sdk iOS 集成

    1添加支付宝源文件和库文件AlipayOrder.h    AlipayOrder.m    AlipayResult.h    AlipayResult.m  AlixLibService.h   ...

  10. [idea]idea配置Jrebel 标签: ideatomcatjrebel 2017-03-14 09:23 547人阅读 评论(21

    上篇博客讲了如何为idea设置tomcat,这篇博客要给大家推荐Jrebel,其实eclipse上也可以配置Jrebel,但是在使用eclipse的时候并没有发现这些东西,还是习惯使然,对一个比较熟悉 ...