首页 / JAVA / java文本文件读写操作
java文本文件读写操作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java文本文件读写操作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2953字,纯文字阅读大概需要5分钟。
内容图文
![java文本文件读写操作](/upload/InfoBanner/zyjiaocheng/736/59d39eebd0024d1a8f0331a2ca246217.jpg)
FileOpUtil.java
package com.qiaojun;
import com.jmt.alipay.property.SystemProps;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
/**
* 文件读写工具类
* @author qiaojun
* @date 2019/4/3
*/
@Slf4j
public class FileOpUtil {
/**
* 下载文件到指定位置
* @param downLoadUrl 下载链接
* @param saveUrl 保证地址
*/
public static void downLoadBillData(String downLoadUrl,String saveUrl){
try {
InputStream in = new URL(downLoadUrl).openConnection().getInputStream();
OutputStream ou = new FileOutputStream(saveUrl);
int c = 0;
int k = 0;
float m = 0;
int bread = 0;
byte[] byt = new byte[2048];
while ((bread = in.read(byt, 0, 2048)) != -1) {
ou.write(byt, 0, bread);
c = c + bread;
k = c / 1024;
m = k / 1024;
System.out.println("己下载: " + k + "k " + m + "M");
}
in.close();
ou.close();
} catch (Exception e) {
e.printStackTrace();
log.info("------------下载文件出错------------------");
//发邮件告警
MailUtl.sendSimpleMail("下载文件出错-"+ SystemProps.CURRENT_ENV,"异常信息:\n"+e.getMessage(),null,null);
}
}
/**
* 读取压缩包里面的文本文件
*/
public static String readZipBillFile(String file) throws IOException {
ZipFile zf = new ZipFile(file,Charset.forName("GBK"));
InputStream in = new BufferedInputStream(new FileInputStream(file));
ZipInputStream zin = new ZipInputStream(in,Charset.forName("GBK"));
ZipEntry ze;
StringBuilder resultStr = new StringBuilder();
while ((ze = zin.getNextEntry()) != null) {
//如果不是目录才处理
if (!ze.isDirectory()) {
// System.out.println("file - " + ze.getName() + " : "+ ze.getSize() + " bytes");
System.out.println("文件名: " +ze.getName());
long size = ze.getSize();
if (size > 0) {
BufferedReader br = new BufferedReader(
new InputStreamReader(zf.getInputStream(ze),Charset.forName("GBK")));
String line;
boolean flag = false;
while ((line = br.readLine()) != null) {
resultStr.append(line).append("\n");
}
br.close();
}
}
}
zin.closeEntry();
in.close();
zf.close();
return resultStr.toString();
}
/**
* 写入文本文件内容
* @param filePath 写入地址
* @param content 写入内容
*/
public static void writeJSONToFileTxt(String filePath,String content){
FileOutputStream out;
PrintStream p;
try {
out = new FileOutputStream(filePath);
p = new PrintStream(out);
p.println(content);
p.close();
out.close();
} catch (Exception e) {
log.error("-----写入文本文件错误filePath:"+filePath+";异常信息:"+e.getMessage(),e);
}
}
/**
* 读取文件内容
* @param filePath 文件地址
* @return 内容
*/
public static String readTxtFile(String filePath) {
StringBuilder line = new StringBuilder();
try {
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
BufferedReader br = new BufferedReader(
new InputStreamReader(in, Charset.forName("UTF-8")));
String str;
while ((str = br.readLine()) != null) {
line.append(str);
}
in.close();
br.close();
} catch (Exception e) {
log.error("-----读取文本文件错误---:"+e.getMessage(),e);
}
return line.toString();
}
}
内容总结
以上是互联网集市为您收集整理的java文本文件读写操作全部内容,希望文章能够帮你解决java文本文件读写操作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。