代码如下:

 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. 使用idea工具的几个个性化步骤

    1.更改背景样式2.添加激情代码插件 Power mode II3.安装省略 getset 插件 Lombok 引入pom.xml <!-- 此组件可以用来实体类 省略 getset 构造等等 ...

  2. js关闭或者刷新页面后执行事件

    onbeforeunload 使用方法 window.onbeforeunload=function(){ return ''; } 有返回值才能弹出显示,或者有需要执行的事件也行.

  3. Git-svn:用git管理svn仓库

    1. 将svn仓库中的项目导入本地git仓库 使用 git svn clone [svn_url] 命令即可完成从svn仓库导入本地,由于该命令会将svn仓库中所有版本的更新都会同步到本地仓库,如果项 ...

  4. JQuery--jQquery控制CSS样式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Ceph 之Multisite 下的bucket reshard

    目录 一.背景和问题 二.bucket reshard 过程 主集群信息汇总 Multisite 下手动reshard References 一.背景和问题 默认情况下只有当单个bucket承载的ob ...

  6. PHPCMS快速建站系列之类别调用及类别显示页面

    在需要调用类别的地方,比如列表页,首先写循环前面写上一句: <?php $TYPE = getcache('type_content','commons');?> 这句就是把类别缓存加载进 ...

  7. NPOI操作、导出Excel

    //使用NPOI操作Excel private void ExcelNPOI(System.Data.DataTable dt, HttpContext context) { IWorkbook wo ...

  8. 2019-4-10-VisualStudio-2019-尝试使用-C#-8.0-新的方式

    title author date CreateTime categories VisualStudio 2019 尝试使用 C# 8.0 新的方式 lindexi 2019-04-10 10:41: ...

  9. idea建立maven聚合项目 标签: mavenidea 2017-01-08 15:33 2477人阅读 评论(30)

    上篇文章写了如何用idea建立maven项目,idea建立maven聚合项目我感觉不如eclipse方便,不过并不是没有办法,下面写一下这个小教程. 建立maven project 建立maven p ...

  10. jQuery 图片跟着鼠标动

    html默认鼠标样式改成图片时格式为 .ani 图片跟随鼠标挪动 html <div id="mouseImg"> <img src="images/问 ...