📄 fileutil.java
字号:
/* * $Id: FileUtil.java 7217 2006-04-06 18:55:22Z jaz $ * * Copyright (c) 2001-2006 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */package org.ofbiz.base.util;import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.Writer;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.BufferedReader;/** * File Utilities * * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 7217 $ * @since 3.5 */public class FileUtil { public static final String module = FileUtil.class.getName(); public static void writeString(String fileName, String s) throws IOException { writeString(null, fileName, s); } public static void writeString(String path, String name, String s) throws IOException { Writer out = getBufferedWriter(path, name); try { out.write(s + System.getProperty("line.separator")); } catch (IOException e) { Debug.logError(e, module); throw e; } finally { if (out != null) { try { out.close(); } catch (IOException e) { Debug.logError(e, module); } } } } public static Writer getBufferedWriter(String path, String name) throws IOException { String fileName = getPatchedFileName(path, name); if (UtilValidate.isEmpty(fileName)) { throw new IOException("Cannot obtain buffered writer for an empty filename!"); } return new BufferedWriter(new FileWriter(fileName)); } public static String getPatchedFileName(String path, String fileName) throws IOException { // make sure the export directory exists if (UtilValidate.isNotEmpty(path)) { path = path.replaceAll("\\\\", "/"); File parentDir = new File(path); if (!parentDir.exists()) { if (!parentDir.mkdir()) { throw new IOException("Cannot create directory for path: " + path); } } // build the filename with the path if (!path.endsWith("/")) { path = path + "/"; } if (fileName.startsWith("/")) { fileName = fileName.substring(1); } fileName = path + fileName; } return fileName; } public static StringBuffer readTextFile(String fileName, boolean newline) throws FileNotFoundException, IOException { File file = new File(fileName); if (!file.exists()) { throw new FileNotFoundException(); } StringBuffer buf = new StringBuffer(); BufferedReader in = null; try { in = new BufferedReader(new FileReader(file)); String str; while ((str = in.readLine()) != null) { buf.append(str); if (newline) { buf.append(System.getProperty("line.separator")); } } } catch (IOException e) { Debug.logError(e, module); throw e; } finally { if (in != null) { try { in.close(); } catch (IOException e) { Debug.logError(e, module); } } } return buf; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -