In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what are the commonly used file manipulation methods in java. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Common operation methods of files
Public class FileUtility {
/ * *
* create a directory
*
* @ param fileDir
* the directory where the file is located
*
* @ throws java.lang.Exception
, /
Public static void createDir (String filePath) {
File fileDir = new File (filePath)
Try {
If (! fileDir.exists ()) {
FileDir.mkdir ()
}
} catch (Exception ex) {
}
}
/ * *
* @ roseuid 46345A88024A
, /
Public FileUtility () {
}
/ * *
* if the file exists, delete it and recreate it; if it does not exist, create it.
*
* @ param filePath-
* File path
* @ param fileName-
* File name
* @ param sb-
* output content
* @ roseuid 4612F3C103B9
, /
Public static void createFile (String filePath, String fileName
StringBuffer sb) {
CreateDir (filePath)
Java.io.File file = new java.io.File (filePath, fileName)
Try {
If (file.exists ()) = = true) {
File = new java.io.File (filePath, fileName)
File.delete ()
}
File.createNewFile ()
FileOutputStream fos = new FileOutputStream (file)
Writer out = new OutputStreamWriter (fos, "GB2312")
Out.write (sb.toString ())
Out.flush ()
Out.close ()
Fos.close ()
Out = null
Fos = null
} catch (Exception ex) {
}
}
/ * *
* Delete files
*
* @ param filePath-
* File path
* @ param fileName-
* File name
* @ roseuid 4612F3C103B9
, /
Public static void deleteFile (String filePath, String fileName)
{
CreateDir (filePath)
Java.io.File file = new java.io.File (filePath, fileName)
Try {
If (file.exists ()) {
File = new java.io.File (filePath, fileName)
File.delete ()
}
File = null
} catch (Exception ex) {
}
}
/ * *
* create a new file
* @ param filePath
* @ param fileName
, /
Public static void createOrClearFile (String filePath, String fileName)
{
CreateDir (filePath)
File fileDS = new File (filePath, fileName)
Try {
If (fileDS.exists ()) {
FileDS = new File (filePath, fileName)
FileDS.delete ()
}
FileDS.createNewFile ()
} catch (Exception ex) {
}
}
/ * *
* add content to the document
* @ param filePath
* @ param fileName
* @ param sb
, /
Public static void writeAppendFile (String filePath, String fileName
StringBuffer sb) {
Try {
FileWriter fileWriter = new FileWriter (filePath + File.separator
+ fileName, true)
PrintWriter out = new PrintWriter (fileWriter)
Out.print (sb.toString ())
Out.flush ()
FileWriter.flush ()
Out.close ()
FileWriter.close ()
Out = null
FileWriter = null
} catch (Exception ex) {
}
}
/ * *
* compress the files in the specified directory
*
* @ param zipList-
* File name, the file name in the list is compressed into a compressed package
* @ param filePath-
* File path
* @ param zipName-
* name of the compressed file
* @ roseuid 4612F4AE03B1
, /
Public static void zip (List zipList, String filePath, String zipName)
{
BufferedInputStream origin = null
ZipOutputStream out = null
Int BUFFER = 2048
CreateDir (filePath)
Try {
File zipFile = new File (filePath, zipName)
FileOutputStream dest = new FileOutputStream (zipFile)
Byte data [] = new byte [BUFFER]
/ / enter the flag bit for the compressed file, judge whether it is damaged when decompressing, and do not decompress if it is damaged.
Out = new ZipOutputStream (new BufferedOutputStream (dest))
/ / set the file to be compressed
Out.setMethod (ZipOutputStream.DEFLATED)
Int num = zipList.size ()
For (int I = 0; I
< num; i++) { FileInputStream fi = new FileInputStream(filePath + File.separator + (String) zipList.get(i)); origin = new BufferedInputStream(fi, BUFFER); ZipEntry entry = new ZipEntry((String) zipList.get(i)); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } origin.close(); } out.close(); } catch (Exception e) { } finally { origin = null; out = null; } } /** * 删除临时文件 * * @param delList - * 要删除的文件的名称的列表 * @param filePath - * 删除文件的路径 * @roseuid 4612F5380341 */ public static void delete(List delList, String filePath) { try { for (int i = 0; i < delList.size(); i++) { String fileName = (String) delList.get(i); File f = new File(filePath, fileName); if (f.exists()) { f.delete(); } fileName = null; f = null; } } catch (Exception e) { } } /** * 从zip文件中获取指定名称的文件 * * @param filePath - * zip所在文件目录 * @param zipFileName - * zip文件名称 * @param fileName - * 要读出的文件名称 * @return BufferedReader * @roseuid 4612F788032E */ public static BufferedReader getZipReader(String filePath, String zipFileName, String fileName) { BufferedReader br = null; try { ZipFile zf = new ZipFile(filePath + File.separator + zipFileName); ZipEntry entry = zf.getEntry(fileName); br = new BufferedReader(new InputStreamReader(zf .getInputStream(entry))); } catch (Exception e) { } return br; } public static Document readXmlFile(File xmlFile) { Document doc = null; try { SAXBuilder saxBuilder = new org.jdom.input.SAXBuilder(false); doc = saxBuilder.build(xmlFile); } catch (Exception ex) { } return doc; } /** * 保存导出文件 * * @param saveDir * String * @param fileName * String * @param doc * Document */ public static void saveXmlFile(String saveDir, String fileName, Document doc) { createDir(saveDir); try { java.io.FileWriter fw = null; java.io.BufferedWriter bw = null; File file = new File(saveDir, fileName); if (file.exists()) { file.delete(); } if (file.createNewFile()) { XMLOutputter xmlOut = new XMLOutputter(" ", true); xmlOut.setEncoding("GB2312"); fw = new java.io.FileWriter(file); bw = new java.io.BufferedWriter(fw); xmlOut.output(doc, bw); bw.flush(); fw.flush(); bw.close(); fw.close(); bw = null; fw = null; } } catch (Exception ex) { } } public static void createEntryFile() { try { // 创建xml文件 Element rootElement = new Element("entry"); Document doc = new Document(rootElement); FileUtility.saveXmlFile(FilePath, FileName, doc); } catch (Exception e) { } } public void zip(List tableList, String entryFileName, String backupFilePath, String backupFileName){ // 1.获取所有的压缩文件名 List fileList = new ArrayList(); int num = tableList.size(); TableObject to = null; for (int i = 0; i < num; i++) { to = (TableObject) tableList.get(i); //只有记录大于0的表才进行输出 if (to.getRecordnum() >0) {
FileList.add (to.getBackupfile ())
}
}
FileList.add (entryFileName)
/ / 2. Compress all files and delete temporary files
Try {
FileUtility.zip (fileList, backupFilePath, backupFileName)
FileUtility.delete (fileList, backupFilePath)
} catch (Exception e) {
}
}
/ * *
* check whether the path is legal and throw an exception
* @ param path
* @ return
, /
Public static String checkPath (String path) {
If (! path.matches ("[^ *?"
< >|] {1,} ") {
} else {
File dir = new File (path)
If (! dir.exists ()) {
}
Return dir.getAbsolutePath () + File.separator
}
}
}
On "what are the commonly used file manipulation methods in java" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.