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()方法为抽 ...
随机推荐
- Excel柱状图折线图组合怎么做 Excel百分比趋势图制作教程
Excel柱状图折线图组合怎么做 Excel百分比趋势图制作教程 用excel作图时候经常会碰到做柱状图和折线图组合,这样的图一般难在折线图的数据很小,是百分比趋势图,所以经常相对前面主数据太小了,在 ...
- 【JZOJ5060】【GDOI2017第二轮模拟day1】公路建设 线段树+最小生成树
题面 在Byteland一共有n 个城市,编号依次为1 到n,它们之间计划修建m条双向道路,其中修建第i 条道路的费用为ci. Byteasar作为Byteland 公路建设项目的总工程师,他决定选定 ...
- SpringBoot实战之异常处理篇
在互联网时代,我们所开发的应用大多是直面用户的,程序中的任何一点小疏忽都可能导致用户的流失,而程序出现异常往往又是不可避免的,那该如何减少程序异常对用户体验的影响呢?其实方法很简单,对异常进行捕获,然 ...
- php表单和缩略图处理类是什么样呢
<?php//封装一个表单验证类//中文验证.邮箱验证.电话号码.手机.QQ.身份证.(由字母.数字.下划线组成,不能以数字开头)header('content-type:text/html;c ...
- CSS3圆环旋转效果
html结构: <div class="demo"></div> css结构: .demo{ width:250px; height:250px; bord ...
- Java Map 排序
1. 按照key值排序 对于java中Map的排序,有排序Map,比如TreeMap,对于这个Map,首先只能按照键排序,其次再put和remove的时候由于需要排序,性能上会有所牺牲. 这种方案,使 ...
- React-FlipOver-Counter(日历翻页)
跟窝一起学习鸭~~ //index.js import React from 'react'; import ReactDOM from 'react-dom'; import './index.cs ...
- 简单线性回归(最小二乘法)python实现
简单线性回归(最小二乘法)¶ 0.引入依赖¶ In [7]: import numpy as np import matplotlib.pyplot as plt 1.导入数据¶ In [ ...
- 【AtCoder Regular Contest 092】C.2D Plane 2N Points【匈牙利算法】
C.2D Plane 2N Points 题意:给定N个红点二维坐标N个蓝点二维坐标,如果红点横纵坐标都比蓝点小,那么它们能够构成一组.问最多能构成多少组. 题解:把满足要求的红蓝点连线,然后就是匈牙 ...
- 预警| Confluence 高危漏洞被大规模利用,阿里云WAF接入即可防护,支持免费应急服务
2019年4月4日,阿里云安全应急响应中心监测到Confluence 官方发布安全更新指出,Widget Connector 存在服务端模板注入漏洞,攻击者能利用此漏洞实现目录穿越遍历甚至远程命令执行 ...