java 文件读写工具 FileUtil
代码如下:
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的更多相关文章
- java文件读写工具类
依赖jar:commons-io.jar 1.写文件 // by FileUtilsList<String> lines = FileUtils.readLines(file, " ...
- java 文件读写的有用工具
java 文件读写的有用工具 package org.rui.io.util; import java.io.BufferedReader; import java.io.File; import j ...
- java文件处理工具类
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedRead ...
- properties文件读写工具类
java代码: import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; ...
- Spring-Boot ☞ ShapeFile文件读写工具类+接口调用
一.项目目录结构树 二.项目启动 三.往指定的shp文件里写内容 (1) json数据[Post] { "name":"test", "path&qu ...
- android 文件读写工具类
将可以序列化的对象通过base64编码后进行保存 但是感觉多数情况下,不需要采用这个功能,直接保存原始的json字符串,取出来之后再进行解析即可 package com.wotlab.home.mon ...
- list集合、txt文件对比的工具类和文件读写工具类
工作上经常会遇到处理大数据的问题,下面两个工具类,是在处理大数据时编写的:推荐的是使用map的方式处理两个list数据,如果遇到list相当大数据这个方法就起到了作用,当时处理了两个十万级的list, ...
- java文件读写的两种方式
今天搞了下java文件的读写,自己也总结了一下,但是不全,只有两种方式,先直接看代码: public static void main(String[] args) throws IOExceptio ...
- java文件读写操作
Java IO系统里读写文件使用Reader和Writer两个抽象类,Reader中read()和close()方法都是抽象方法.Writer中 write(),flush()和close()方法为抽 ...
随机推荐
- Loadrunner常用分析点
Loadrunner常用的分析点 一.在Vuser(虚拟用户状态)中 1.Running Vusers:提供了生产负载的虚拟用户运行状态的相关信息,可以帮助我们了解负载生成的结果.(即用户在几分钟左右 ...
- SQL注入绕过的技巧总结
sql注入在很早很早以前是很常见的一个漏洞.后来随着安全水平的提高,sql注入已经很少能够看到了.但是就在今天,还有很多网站带着sql注入漏洞在运行.稍微有点安全意识的朋友就应该懂得要做一下sql注入 ...
- The 16th UESTC Programming Contest Final 游记
心情不好来写博客. 为了满足ykk想要气球的愿望,NicoDafaGood.Achen和我成功去神大耍了一圈. 因为队名一开始是LargeDumpling应援会,然后队名被和谐,变成了学校的名字,顿时 ...
- MySQL非安装版安装
1 数据库的打开方式(非安装版本) 1.解压mysql-5.7.12-winx64.zip到一个路径上没有空格没有汉字的目录中 2.复制my-default.ini重命名为my.ini 3.命令行进入 ...
- 如何在Liferay 7中创建一个简单的JSF Portlet
这个将在Liferay IDE 3.1 M3的发布版中提供创建的选项,但是你也可以通过命令行来创建. 1.这是Liferay JSF团队的官网:http://liferayfaces.org/ 你能在 ...
- Timer和DispatcherTimer简单使用
1. 设定计时器相关属性,使用委托方法处理事件触发 DispatcherTimer dispatcherTimer= new DispatcherTimer(); dispatcherTimer.Ti ...
- nodeJs学习-11 multer中间件,解析post文件,上传文件
const express=require('express'); const bodyParser=require('body-parser'); const multer=require('mul ...
- UVA_490:Rotating Sentences
"R Ie n te h iD ne kc ,a r tt he es r eo fn oc re e s Ia i ad m, . ...
- xcode禁用代码分析的警告和内存泄漏
在使用xcode进行iphone应用开发时,经常需要添加一些第三方的类库,而一些第三方的类库由于缺少维护,从而导致类库中含有各种警告和各种内存泄漏,但并不影响运行. 倘若我们需要用到第三方库,而由不想 ...
- sql常用内置函数
用于测试的表: 一.SUM 返回数值列的总数. 执行查询: select SUM(Score) as 总得分 from Students 效果: 二.MAX 返回一列中的最大值.. 执行查询: sel ...