当前位置:网站首页>File common tool class, stream related application (record)
File common tool class, stream related application (record)
2022-06-29 15:20:00 【He Xiaoshu】
File Class common method records ,IO Stream application ,( Compressed file processing ,url File download ) etc. ...
import java.io.File; // Practical methods .
// createNewFile() throws IOException Create a new file
File file = new File("E:/ A new file .txt");
// mkdirs() Create multi-level directory
File file = new File("E:/aa/bb/cc");
File file = new File("E:/Uicode.dat");
boolean file1 = file.isFile();// Whether it is - file
boolean directory = file.isDirectory();// Whether it is - Catalog
boolean exists = file.exists();// Determine whether a file or directory exists
boolean b = file.canRead();// Determine whether the file is readable
boolean b1 = file.canWrite();// Judge whether the document is writable
boolean hidden = file.isHidden();// Determine whether the file is hidden
// Value
String absolutePath = file.getAbsolutePath();// Get absolute path
String path = file.getPath();// Get relative path
String name = file.getName();// Get the file or directory name
long length = file.length();// Get file size
long l = file.lastModified();// Get the last modification time of the file , Millisecond time stamp .
String parent = file.getParent();// Get the folder name
System.out.println(" Absolute path "+absolutePath);
System.out.println(" Relative paths "+path);
System.out.println(" File catalog name "+name);
System.out.println(" file size "+length);
System.out.println(" Last modification time "+ new Date(l));
System.out.println(" Get the name of the folder "+parent);
IO flow . Byte stream : The most primitive stream , The data read out is 010101 This lowest level of data representation , It's just that it's read in bytes , A byte (byte) yes 8(bit), Reading is a byte by byte .
Character stream : Read data out character by character .1 character 2 Bytes
Say input and output , All are For programs !!
Node flow : From a specific data source ( node ) Read and write data .( A pipe )
Processing flow : Is on top of an existing stream , Through the processing of data for the program to provide a more powerful read-write function .( The pipe is wrapped on the pipe )

// Input stream
InputStream
// Common methods return -1 End of input stream
int read() throws IOException
String readLine();// Line by line reading
synchronized void mark(int var1)// Make a mark
synchronized void reset()// Go back to the tag
// Close the stream to free up memory resources
void close()
// Output stream
OutputStream
void write(int b) throws IOException
void newLine();// Line break
void flush() throws IOException
void close() throws IOException
// example
while ((b=in.read())!= -1){
out.write(b);
}
Utility class
/** * File compression . Compress to the same level directory by default ( Encryption is not supported ) * @param sourceFileName Source file ,eg:E:/text.txt * @param format Compressed format rar zip * @return result * @throws Exception */
public static Result compressFile(String sourceFileName,String format) throws Exception {
String fileSuffix; file extension eg: .txt
String zipName;// Compressed file name
File file = new File(sourceFileName);
if (sourceFileName.lastIndexOf(".") != -1){
// Is it a directory
fileSuffix = sourceFileName.substring(sourceFileName.lastIndexOf("."));// suffix
zipName=sourceFileName.replaceAll(fileSuffix,"")+"."+format;
}else {
zipName=file.getParent()+File.separator+sourceFileName.substring(sourceFileName.lastIndexOf("/")).replaceAll("/","")+"."+format;
}
long startTime=System.currentTimeMillis();
// establish zip Output stream
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipName));
File sourceFile = new File(sourceFileName);
// Call function
compress(out, sourceFile, sourceFile.getName());// sourceFile.getName() E:/aa/bb name by aa.
out.close();
long endTime=System.currentTimeMillis();
logger.info(" Compression complete !"+", Time consuming :"+(endTime-startTime)+" millisecond ");
logger.info(" The output path :"+zipName);
return Result.ok(" Compression complete "+", Time consuming :"+(endTime-startTime)+" millisecond ");
}
public static void compress(ZipOutputStream out, File sourceFile, String base) throws Exception {
// If the path is Directory ( Folder )
if(sourceFile.isDirectory()) {
// Take out the files in the folder ( Or subfolders )
File[] flist = sourceFile.listFiles();
if(flist.length==0) {
// If the folder is empty , Then just at the destination zip Write a directory entry point to the file
System.out.println(base + File.separator);
out.putNextEntry(new ZipEntry(base + File.separator));
} else {
// If the folder is not empty , Then recursively call compress, Every file in the folder ( Or folder ) Compress
for(int i=0; i<flist.length; i++) {
compress(out, flist[i], base+File.separator+flist[i].getName());
}
}
} else {
out.putNextEntry(new ZipEntry(base));
FileInputStream fos = new FileInputStream(sourceFile);
BufferedInputStream bis = new BufferedInputStream(fos);
int len;
byte[] buf = new byte[1024];
System.out.println(base);
while((len=bis.read(buf, 0, 1024)) != -1) {
out.write(buf, 0, len);
}
bis.close();
fos.close();
}
}
/** * HAOGE version * Decompress the package , Decryption is not supported , Support rar,zip Format * @param srcFile Source file * @param destDirPath The output directory */
public static Result uncompressFile(File srcFile,String destDirPath){
long start = System.currentTimeMillis();
// Determine whether the source file exists
if (!srcFile.exists()) {
return Result.error(srcFile.getPath() + " Source file does not exist ");
}
// Start decompressing
ZipFile zipFile = null;
try {
zipFile = new ZipFile(srcFile);
Enumeration<?> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
System.out.println(" decompression " + entry.getName());
// If it's a folder , Just create a folder
if (entry.isDirectory()) {
String dirPath = destDirPath + "/" + entry.getName();
File dir = new File(dirPath);
dir.mkdirs();
} else {
// If it's a file , Just create a file , And then use io Streaming content copy In the past
File targetFile = new File(destDirPath + "/" + entry.getName());
// Ensure that the parent folder of this file must exist
if(!targetFile.getParentFile().exists()){
targetFile.getParentFile().mkdirs();
}
targetFile.createNewFile();
// Write the contents of the compressed file to this file
InputStream is = zipFile.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(targetFile);
int len;
byte[] buf = new byte[1024];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
// Closure sequence , Open first and then close
fos.close();
is.close();
}
}
long end = System.currentTimeMillis();
logger.info(" Unzip complete , Time consuming :" + (end - start) +" millisecond ");
return Result.ok(" Unzip complete , Time consuming :" + (end - start) +" millisecond ");
} catch (IOException e) {
e.printStackTrace();
} finally {
if(zipFile != null){
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return Result.error(" Decompression failed ");
}
/** * decompression .gz Format , Compressed files * @param inFileName eg: E:\\2019072308.gz */
public static void uncompressGz(String inFileName) {
try {
if (!getExtension(inFileName).equalsIgnoreCase("gz")) {
System.err.println(" The document is not gz Type zip ");
System.exit(1);
}
System.out.println(" Start extracting files ....");
Long startTime=System.currentTimeMillis();
GZIPInputStream in = null;
try {
in = new GZIPInputStream(new FileInputStream(inFileName));
} catch(FileNotFoundException e) {
System.err.println("File not found. " + inFileName);
System.exit(1);
}
System.out.println("Open the output file.");
String outFileName = getFileName(inFileName);
FileOutputStream out = null;
try {
out = new FileOutputStream(outFileName);
} catch (FileNotFoundException e) {
System.err.println("Could not write to file. " + outFileName);
System.exit(1);
}
System.out.println("Transfering bytes from compressed file to the output file.");
byte[] buf = new byte[1024];
int len;
while((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
Long endTime=System.currentTimeMillis();
System.out.println(" Unzip complete , Time consuming :"+(startTime-endTime)+" millisecond ");
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
/** * Get file extension * @param f E:\\2019072308.gz * @return <code>String</code> representing the extension of the incoming * file. */
public static String getExtension(String f) {
String ext = "";
int i = f.lastIndexOf('.');
if (i > 0 && i < f.length() - 1) {
ext = f.substring(i+1);
}
return ext;
}
/** * Used to extract file names without extensions * @param f Incoming file to get the filename * @return <code>String</code> representing the filename without its * extension. */
public static String getFileName(String f) {
String fname = "";
int i = f.lastIndexOf('.');
if (i > 0 && i < f.length() - 1) {
fname = f.substring(0,i);
}
return fname;
}
/** * Read file data in behavioral units . * Use BufferedReader Read files in line units , Read to the last line . * @param is file IO flow * @param c cache Use BufferedReader, By default, each read in (5 * 1024 * 1024)5M data . Reduce IO * @return List<String> */
public static List<String> readFileContent(InputStream is, int c) {
BufferedReader reader = null;
List<String> listContent = new ArrayList<>();
try {
BufferedInputStream bis = new BufferedInputStream(is);
reader = new BufferedReader(new InputStreamReader(bis, "utf-8"), c==0?5 * 1024 * 1024:c);// cache
String tempString = null;
// Read in one line at a time , Until read in null End of file
while ((tempString = reader.readLine()) != null) {
listContent.add(tempString);
}
is.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return listContent;
}
/** * Writes content to a file in behavioral units * @param contest Content collection * @param os new FileOutputStream(filePath) * @param c c cache Use BufferedReader, By default, each read in (5 * 1024 * 1024)5M data . Reduce IO */
public static void writeFileContent(List<String> contest,OutputStream os,int c){
BufferedWriter bw;
try {
BufferedOutputStream bos = new BufferedOutputStream(os);
bw = new BufferedWriter(new OutputStreamWriter(bos, StandardCharsets.UTF_8), c==0?5 * 1024 * 1024:c);
for(String str:contest){
bw.write(str);
bw.newLine();
bw.flush();
}
bw.close();
}catch (IOException e){
e.printStackTrace();
}
}
/** * From the network Url Download files from * @param urlStr * @param fileName * @param savePath * @throws IOException */
public static void downLoadFromUrl(String urlStr,String fileName,String savePath) throws IOException{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
// Set the timeout to 3 second
conn.setConnectTimeout(3*1000);
// Prevent shield program from grabbing and returning 403 error
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
// Get the input stream
InputStream inputStream = conn.getInputStream();
// Get your own array
byte[] getData = readInputStream(inputStream);
// File save location
File saveDir = new File(savePath);
if(!saveDir.exists()){
saveDir.mkdir();
}
File file = new File(saveDir+ File.separator+fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if(fos!=null){
fos.close();
}
if(inputStream!=null){
inputStream.close();
}
System.out.println("info:"+url+" download success");
}
/** * Get byte array from input stream * @param inputStream * @return * @throws IOException */
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
/** * File download , The browser pops up the download window , type * @param response response * @param AbsolutePath Absolute path , The full path eg:E://a.text ; * @param FileName File name eg: a.text; */
public static void downLoadFile(HttpServletResponse response, HttpServletRequest request, String AbsolutePath, String FileName) throws Exception{
// Get... By file name MIME type
String contentType = request.getServletContext().getMimeType(FileName);
String contentDisposition = "attachment;filename=" + filenameEncoding(FileName, request);
// File streaming
FileInputStream input = new FileInputStream(AbsolutePath);
// Set head
response.setHeader("Content-Type", contentType);
response.setHeader("Content-Disposition", contentDisposition);
// Get the flow bound to the response end
ServletOutputStream output = response.getOutputStream();
// Write the data in the input stream to the output stream .--- The node of the output stream is the client
IOUtils.copy(input, output);
input.close();
}
/** * Download file code * @param filename File name * @param request request * @throws IOException */
public static String filenameEncoding(String filename, HttpServletRequest request) throws IOException {
String agent = request.getHeader("User-Agent"); // Get browser
if (agent.contains("Firefox")) {
BASE64Encoder base64Encoder = new BASE64Encoder();
filename = "=?utf-8?B?"+ base64Encoder.encode(filename.getBytes("utf-8"))+ "?=";
} else if(agent.contains("MSIE")) {
filename = URLEncoder.encode(filename, "utf-8");
} else {
filename = URLEncoder.encode(filename, "utf-8");
}
return filename;
}
Application : Business scenario , call API, return JSON in , There is a link to download chat records , When downloading files .gz Compressed package
–. decompression , Reading documents , Handle JSON data , turn list
public static void main(String[] args) throws Exception{
try{
// download , Compressed package
FileUtil.downLoadFromUrl("http://ebs-chatmessage-a1.easemob.com/history/3D/1111170113115695/xianglekang/2019072613.gz?Expires=1564124524&OSSAccessKeyId=LTAIlKPZStPokdA8&Signature=1fYR1i3pf%2FpRfJPqR882wIEwuWk%3D",
"2019072613.gz","E:/");
}catch (Exception e) {
e.printStackTrace();
}
// decompression
FileUtil.uncompressGz("E:\\2019072613.gz");
List<String> list = FileUtil.readFileContent(new FileInputStream("E:/2019072613"), 5);
List<HxMsg> msgList= new ArrayList<>();
HxMsg hxMsg = new HxMsg();
for (String s:list ) {
JSONObject jsonObject=JSONObject.parseObject(s);
hxMsg.setChatType(jsonObject.getString("chat_type"));
hxMsg.setMsg(JSONObject.parseArray(jsonObject.getJSONObject("payload").getString("bodies")).getJSONObject(0).getString("msg"));
hxMsg.setType(JSONObject.parseArray(jsonObject.getJSONObject("payload").getString("bodies")).getJSONObject(0).getString("type"));
hxMsg.setFrom(jsonObject.getString("from"));
hxMsg.setTo(jsonObject.getString("to"));
hxMsg.setTimesTamp(jsonObject.getString("timestamp"));
msgList.add(hxMsg);
hxMsg = new HxMsg();
}
System.out.println(msgList.toString());
}
边栏推荐
猜你喜欢

Lumiprobe reactive dye miscellaneous dye: BDP FL ceramide

What is the relationship between synchronized and multithreading

Flink SQL任务TaskManager内存设置

const用法精讲

Informatics Olympiad all in one 1194: mobile route
![Abnormal logic reasoning problem of Huawei software test written test [2] Huawei hot interview problem](/img/f0/5c2504d51532dcda0ac115f3703384.gif)
Abnormal logic reasoning problem of Huawei software test written test [2] Huawei hot interview problem

信息学奥赛一本通2061:梯形面积

MySQL JSON array operation JSON_ array_ append、json_ array_ insert

EMC surge protection and decoupling design

For example, the visual appeal of the live broadcast of NBA Finals can be seen like this?
随机推荐
Unity C basic review 27 - delegation example (p448)
Is Guangzhou futures regular? If someone asks you to log in with your own mobile phone and help open an account, is it safe?
阿尔兹海默病智能诊断
Const usage
数字图像处理复习
深度学习网络的训练方式
深度学习遥感数据集
LeetCode-1188. 设计有限阻塞队列
Lumiprobe 活性染料丨羧酸:Sulfo-Cyanine7.5羧酸
LeetCode笔记:Biweekly Contest 81
LeetCode笔记:Weekly Contest 299
Review of digital image processing
关于遥感图像解译的思考
雷达的类型
Take another picture of cloud redis' improvement path
render后续来了,封装一个表单往表格中添加数据
材质 动态自发光
MCS: discrete random variables - geometric distribution
Knowledge points: what are the know-how of PCB wiring?
Informatics Olympiad all in one 1003: aligned output