240
src/main/java/com/jsh/erp/utils/ExcelUtils.java
Normal file
240
src/main/java/com/jsh/erp/utils/ExcelUtils.java
Normal file
@@ -0,0 +1,240 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
import jxl.Cell;
|
||||
import jxl.Sheet;
|
||||
import jxl.Workbook;
|
||||
import jxl.format.*;
|
||||
import jxl.write.Label;
|
||||
import jxl.write.WritableCellFormat;
|
||||
import jxl.write.WritableFont;
|
||||
import jxl.write.WritableSheet;
|
||||
import jxl.write.WritableWorkbook;
|
||||
|
||||
public class ExcelUtils {
|
||||
|
||||
public static WritableFont arial14font = null;
|
||||
|
||||
public static File exportObjects(String fileName, String[] names,
|
||||
String title, List<String[]> objects) throws Exception {
|
||||
File excelFile = new File("fileName.xls");
|
||||
WritableWorkbook wtwb = Workbook.createWorkbook(excelFile);
|
||||
WritableSheet sheet = wtwb.createSheet(title, 0);
|
||||
sheet.getSettings().setDefaultColumnWidth(20);
|
||||
WritableFont wfont = new WritableFont(WritableFont.createFont("楷书"), 15);
|
||||
WritableCellFormat format = new WritableCellFormat(wfont);
|
||||
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 20,
|
||||
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
|
||||
jxl.format.Colour.BLACK);
|
||||
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
|
||||
wcfFC.setAlignment(Alignment.CENTRE);
|
||||
wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);
|
||||
// CellView cellView = new CellView();
|
||||
// cellView.setAutosize(true); //设置自动大小
|
||||
format.setAlignment(Alignment.LEFT);
|
||||
format.setVerticalAlignment(VerticalAlignment.TOP);
|
||||
sheet.mergeCells(0, 0, names.length - 1, 0);
|
||||
sheet.addCell(new Label(0, 0, title, wcfFC));
|
||||
int rowNum = 2;
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
sheet.addCell(new Label(i, 1, names[i], format));
|
||||
}
|
||||
for (int j = 0; j < objects.size(); j++) {
|
||||
String[] obj = objects.get(j);
|
||||
for (int h = 0; h < obj.length; h++) {
|
||||
sheet.addCell(new Label(h, rowNum, obj[h], format));
|
||||
}
|
||||
rowNum = rowNum + 1;
|
||||
|
||||
}
|
||||
wtwb.write();
|
||||
wtwb.close();
|
||||
return excelFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出excel,不需要第一行的title
|
||||
*
|
||||
* @param fileName
|
||||
* @param names
|
||||
* @param title
|
||||
* @param objects
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static File exportObjectsWithoutTitle(String fileName,
|
||||
String[] names, String title, List<String[]> objects)
|
||||
throws Exception {
|
||||
File excelFile = new File(fileName);
|
||||
WritableWorkbook wtwb = Workbook.createWorkbook(excelFile);
|
||||
WritableSheet sheet = wtwb.createSheet(title, 0);
|
||||
sheet.getSettings().setDefaultColumnWidth(20);
|
||||
|
||||
// 第一行的格式
|
||||
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 15,
|
||||
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
|
||||
jxl.format.Colour.BLACK);
|
||||
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
|
||||
wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);
|
||||
|
||||
// 设置字体以及单元格格式
|
||||
WritableFont wfont = new WritableFont(WritableFont.createFont("楷书"), 15);
|
||||
WritableCellFormat format = new WritableCellFormat(wfont);
|
||||
format.setAlignment(Alignment.LEFT);
|
||||
format.setVerticalAlignment(VerticalAlignment.TOP);
|
||||
|
||||
// 第一行写入标题
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
sheet.addCell(new Label(i, 0, names[i], wcfFC));
|
||||
}
|
||||
|
||||
// 其余行依次写入数据
|
||||
int rowNum = 1;
|
||||
for (int j = 0; j < objects.size(); j++) {
|
||||
String[] obj = objects.get(j);
|
||||
for (int h = 0; h < obj.length; h++) {
|
||||
sheet.addCell(new Label(h, rowNum, obj[h], format));
|
||||
}
|
||||
rowNum = rowNum + 1;
|
||||
}
|
||||
wtwb.write();
|
||||
wtwb.close();
|
||||
return excelFile;
|
||||
}
|
||||
|
||||
public static String createTempFile(String[] names, String title, List<String[]> objects) throws Exception {
|
||||
File excelFile = File.createTempFile(System.currentTimeMillis() + "", ".xls");
|
||||
WritableWorkbook wtwb = Workbook.createWorkbook(excelFile);
|
||||
WritableSheet sheet = wtwb.createSheet(title, 0);
|
||||
sheet.getSettings().setDefaultColumnWidth(20);
|
||||
WritableFont wfont = new WritableFont(WritableFont.createFont("楷书"), 15);
|
||||
WritableCellFormat format = new WritableCellFormat(wfont);
|
||||
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 20,
|
||||
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
|
||||
jxl.format.Colour.BLACK);
|
||||
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
|
||||
wcfFC.setAlignment(Alignment.CENTRE);
|
||||
wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);
|
||||
// CellView cellView = new CellView();
|
||||
// cellView.setAutosize(true); //设置自动大小
|
||||
format.setAlignment(Alignment.LEFT);
|
||||
format.setVerticalAlignment(VerticalAlignment.TOP);
|
||||
sheet.mergeCells(0, 0, names.length - 1, 0);
|
||||
sheet.addCell(new Label(0, 0, title, wcfFC));
|
||||
int rowNum = 2;
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
sheet.addCell(new Label(i, 1, names[i], format));
|
||||
}
|
||||
for (int j = 0; j < objects.size(); j++) {
|
||||
String[] obj = objects.get(j);
|
||||
for (int h = 0; h < obj.length; h++) {
|
||||
sheet.addCell(new Label(h, rowNum, obj[h], format));
|
||||
}
|
||||
rowNum = rowNum + 1;
|
||||
}
|
||||
wtwb.write();
|
||||
wtwb.close();
|
||||
return excelFile.getName();
|
||||
}
|
||||
|
||||
public static String createCheckRandomTempFile(String[] names, String title, List<String[]> objects,Map<String,String> infoMap) throws Exception {
|
||||
File excelFile = File.createTempFile(System.currentTimeMillis() + "", ".xls");
|
||||
WritableWorkbook wtwb = Workbook.createWorkbook(excelFile);
|
||||
WritableSheet sheet = wtwb.createSheet(title, 0);
|
||||
sheet.getSettings().setDefaultColumnWidth(20);
|
||||
WritableFont wfont = new WritableFont(WritableFont.createFont("楷书"), 14);
|
||||
|
||||
WritableCellFormat format = new WritableCellFormat(wfont);
|
||||
format.setBorder(Border.ALL, BorderLineStyle.THIN);
|
||||
format.setAlignment(Alignment.CENTRE);
|
||||
format.setVerticalAlignment(VerticalAlignment.CENTRE);
|
||||
|
||||
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 20,
|
||||
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
|
||||
jxl.format.Colour.BLACK);
|
||||
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
|
||||
wcfFC.setAlignment(Alignment.LEFT);
|
||||
wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);
|
||||
|
||||
WritableFont nameWfc = new WritableFont(WritableFont.ARIAL, 14,
|
||||
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
|
||||
jxl.format.Colour.BLACK);
|
||||
WritableCellFormat nameFormat = new WritableCellFormat(nameWfc);
|
||||
nameFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
|
||||
nameFormat.setAlignment(Alignment.CENTRE);
|
||||
nameFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
|
||||
|
||||
WritableCellFormat infoFormat = new WritableCellFormat(wfont);
|
||||
infoFormat.setAlignment(Alignment.LEFT);
|
||||
infoFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
|
||||
|
||||
|
||||
sheet.mergeCells(0, 0, names.length - 1, 0);
|
||||
sheet.addCell(new Label(0, 0, infoMap.get("title"), wcfFC));
|
||||
|
||||
sheet.addCell(new Label(0, 2, infoMap.get("info"), infoFormat));
|
||||
sheet.addCell(new Label(2, 2, infoMap.get("dvrnvr"), infoFormat));
|
||||
sheet.addCell(new Label(4, 2, infoMap.get("char"), infoFormat));
|
||||
sheet.addCell(new Label(0, 3, infoMap.get("infoPercent"), infoFormat));
|
||||
sheet.addCell(new Label(2, 3, infoMap.get("dvrnvrPercent"), infoFormat));
|
||||
sheet.addCell(new Label(4, 3, infoMap.get("charPercent"), infoFormat));
|
||||
|
||||
int rowNum = 5;
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
sheet.addCell(new Label(i, 4, names[i], nameFormat));
|
||||
}
|
||||
for (int j = 0; j < objects.size(); j++) {
|
||||
String[] obj = objects.get(j);
|
||||
for (int h = 0; h < obj.length; h++) {
|
||||
sheet.addCell(new Label(h, rowNum, obj[h], format));
|
||||
}
|
||||
rowNum = rowNum + 1;
|
||||
}
|
||||
wtwb.write();
|
||||
wtwb.close();
|
||||
return excelFile.getName();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static String getContent(Sheet src, int rowNum, int colNum) {
|
||||
return src.getRow(rowNum)[colNum].getContents().trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从第i行开始到最后检测指定列的唯一性
|
||||
*
|
||||
* @param src
|
||||
* @param colNum
|
||||
* @param fromRow
|
||||
* 起始行
|
||||
* @return
|
||||
*/
|
||||
public static Boolean checkUnique(Sheet src, int colNum, int fromRow) {
|
||||
Cell[] colCells = src.getColumn(colNum);
|
||||
Set<String> set = new HashSet<String>();
|
||||
for (int i = fromRow; i < colCells.length; i++) {
|
||||
if (!StringUtils.isEmpty(colCells[i].getContents())
|
||||
&& !set.add(colCells[i].getContents())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static File getTempFile(String fileName) {
|
||||
String dir = System.getProperty("java.io.tmpdir"); // 获取系统临时目录
|
||||
return new File(dir + File.separator + fileName);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String msg = "12345";
|
||||
System.out.println(msg.indexOf("@"));
|
||||
}
|
||||
}
|
||||
28
src/main/java/com/jsh/erp/utils/ExportExecUtil.java
Normal file
28
src/main/java/com/jsh/erp/utils/ExportExecUtil.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class ExportExecUtil {
|
||||
|
||||
public static void showExec(File excelFile,String fileName,HttpServletResponse response) throws Exception{
|
||||
response.setContentType("application/octet-stream");
|
||||
fileName = new String(fileName.getBytes("gbk"),"ISO8859_1");
|
||||
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + ".xls" + "\"");
|
||||
FileInputStream fis = new FileInputStream(excelFile);
|
||||
OutputStream out = response.getOutputStream();
|
||||
|
||||
int SIZE = 1024 * 1024;
|
||||
byte[] bytes = new byte[SIZE];
|
||||
int LENGTH = -1;
|
||||
while((LENGTH = fis.read(bytes)) != -1){
|
||||
out.write(bytes,0,LENGTH);
|
||||
}
|
||||
|
||||
out.flush();
|
||||
fis.close();
|
||||
}
|
||||
|
||||
}
|
||||
334
src/main/java/com/jsh/erp/utils/FileUtils.java
Normal file
334
src/main/java/com/jsh/erp/utils/FileUtils.java
Normal file
@@ -0,0 +1,334 @@
|
||||
package com.jsh.erp.utils;
|
||||
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* 文件处理工具类
|
||||
*
|
||||
*/
|
||||
public class FileUtils {
|
||||
|
||||
/**
|
||||
* 功 能: 创建文件夹
|
||||
*
|
||||
* @param path
|
||||
* 参 数:要创建的文件夹名称
|
||||
* @return 返回值: 如果成功true;否则false 如:FileUtils.mkdir("/usr/apps/upload/");
|
||||
*/
|
||||
public static boolean makedir(String path) {
|
||||
File file = new File(path);
|
||||
if (!file.exists())
|
||||
return file.mkdirs();
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存文件
|
||||
*
|
||||
* @param stream
|
||||
* @param path
|
||||
* 存放路径
|
||||
* @param filename
|
||||
* 文件名
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void SaveFileFromInputStream(InputStream stream, String path, String filename)
|
||||
throws IOException {
|
||||
File file = new File(path);
|
||||
boolean flag=true;
|
||||
if(!file.exists()){
|
||||
flag=file.mkdirs();
|
||||
}
|
||||
if(flag){
|
||||
FileOutputStream fs = new FileOutputStream(new File(path+filename));
|
||||
byte[] buffer = new byte[1024 * 1024];
|
||||
int byteread = 0;
|
||||
while ((byteread = stream.read(buffer)) != -1) {
|
||||
fs.write(buffer, 0, byteread);
|
||||
fs.flush();
|
||||
}
|
||||
fs.close();
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 列出某个目录下的所有文件,子目录不列出
|
||||
* @param folderPath:文件夹路径
|
||||
* @return
|
||||
*/
|
||||
public static List<String> listFile(String folderPath){
|
||||
List<String> fileList = new ArrayList<String>(); //FileViewer.getListFiles(destPath, null, false);
|
||||
File f = new File(folderPath);
|
||||
File[] t = f.listFiles();
|
||||
for(int i = 0; i < t.length; i++){
|
||||
fileList.add(t[i].getAbsolutePath());
|
||||
}
|
||||
return fileList;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断文件是否存在
|
||||
*
|
||||
* @param fileName
|
||||
* @return
|
||||
*/
|
||||
public static boolean exists(String fileName) {
|
||||
File file = new File(fileName);
|
||||
if (file.exists()) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取当前路径
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getCurrentPath() {
|
||||
File directory = new File(".");
|
||||
String nowPath = "";
|
||||
try {
|
||||
nowPath = directory.getCanonicalFile().toString();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return nowPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件扩展名
|
||||
*
|
||||
* @param fileName
|
||||
* @return
|
||||
* */
|
||||
public static String getFileExtendName(String fileName) {
|
||||
if (fileName == null) {
|
||||
return "";
|
||||
} else {
|
||||
return fileName.substring(fileName.lastIndexOf(".") + 1, fileName
|
||||
.length());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新文件,如果存在则报错
|
||||
*
|
||||
* @param filePath
|
||||
* @param fileName
|
||||
* @return
|
||||
*/
|
||||
public static void createFile(String filePath, String fileName)
|
||||
throws RuntimeException {
|
||||
String file = null;
|
||||
if (filePath == null) {
|
||||
file = fileName;
|
||||
} else {
|
||||
file = filePath + File.separator + fileName;
|
||||
}
|
||||
createFile(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新文件(含路径),如果存在则报错
|
||||
*
|
||||
* @param fileName
|
||||
* 含有路径的文件名
|
||||
* @return
|
||||
*/
|
||||
public static void createFile(String fileName) throws RuntimeException {
|
||||
File f = new File(fileName);
|
||||
if (f.exists()) {
|
||||
throw new RuntimeException("FILE_EXIST_ERROR");
|
||||
} else {
|
||||
try {
|
||||
File fileFolder = f.getParentFile();
|
||||
if (!fileFolder.exists())
|
||||
fileFolder.mkdirs();
|
||||
f.createNewFile();
|
||||
} catch (IOException ie) {
|
||||
System.out.println("文件" + fileName + "创建失败:" + ie.getMessage());
|
||||
throw new RuntimeException("FILE_CREATE_ERROR");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建目录,如果存在则不创建
|
||||
*
|
||||
* @param path
|
||||
* @return 返回结果null则创建成功,否则返回的是错误信息
|
||||
* @return
|
||||
*/
|
||||
public static String createDir(String path, boolean isCreateSubPah) {
|
||||
String msg = null;
|
||||
File dir = new File(path);
|
||||
|
||||
if (dir == null) {
|
||||
msg = "不能创建空目录";
|
||||
return msg;
|
||||
}
|
||||
if (dir.isFile()) {
|
||||
msg = "已有同名文件存在";
|
||||
return msg;
|
||||
}
|
||||
if (!dir.exists()) {
|
||||
if (isCreateSubPah && !dir.mkdirs()) {
|
||||
msg = "目录创建失败,原因不明";
|
||||
} else if (!dir.mkdir()) {
|
||||
msg = "目录创建失败,原因不明";
|
||||
}
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定目录或文件。 如果要删除是目录,同时删除子目录下所有的文件
|
||||
*
|
||||
* @file:File 目录
|
||||
* */
|
||||
public static void delFileOrFolder(String fileName) {
|
||||
if (!exists(fileName))
|
||||
return;
|
||||
File file = new File(fileName);
|
||||
delFileOrFolder(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定目录或文件。 如果要删除是目录,同时删除子目录下所有的文件
|
||||
*
|
||||
* @file:File 目录
|
||||
* */
|
||||
public static void delFileOrFolder(File file) {
|
||||
if (!file.exists())
|
||||
return;
|
||||
if (file.isFile()) {
|
||||
file.delete();
|
||||
} else {
|
||||
File[] sub = file.listFiles();
|
||||
if (sub == null || sub.length <= 0) {
|
||||
file.delete();
|
||||
} else {
|
||||
for (int i = 0; i < sub.length; i++) {
|
||||
delFileOrFolder(sub[i]);
|
||||
}
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Properties格式配置文件中获取所有参数并保存到HashMap中。
|
||||
* 配置中的key值即map表中的key值,如果配置文件保存时用的中文,则返回结果也会转成中文。
|
||||
*
|
||||
* @param file
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static HashMap readPropertyFile(String file, String charsetName) throws IOException {
|
||||
if (charsetName==null || charsetName.trim().length()==0){
|
||||
charsetName="gbk";
|
||||
}
|
||||
HashMap map = new HashMap();
|
||||
InputStream is =null;
|
||||
if(file.startsWith("file:"))
|
||||
is=new FileInputStream(new File(file.substring(5)));
|
||||
else
|
||||
is=FileUtils.class.getClassLoader().getResourceAsStream(file);
|
||||
Properties properties = new Properties();
|
||||
properties.load(is);
|
||||
Enumeration en = properties.propertyNames();
|
||||
while (en.hasMoreElements()) {
|
||||
String key = (String) en.nextElement();
|
||||
String code = new String(properties.getProperty(key).getBytes(
|
||||
"ISO-8859-1"), charsetName);
|
||||
map.put(key, code);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param path
|
||||
* 文件路径
|
||||
* @param suffix
|
||||
* 后缀名
|
||||
* @param isdepth
|
||||
* 是否遍历子目录
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List getListFiles(String path, String suffix, boolean isdepth) {
|
||||
File file = new File(path);
|
||||
return FileUtils.listFile(file, suffix, isdepth);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param f
|
||||
* @param suffix:后缀名
|
||||
* @param isdepth:是否遍历子目录
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List listFile(File f, String suffix, boolean isdepth) {
|
||||
// 是目录,同时需要遍历子目录
|
||||
List<String> fileList = new ArrayList<String>();
|
||||
if (f.isDirectory() && isdepth == true) {
|
||||
File[] t = f.listFiles();
|
||||
for (int i = 0; i < t.length; i++) {
|
||||
listFile(t[i], suffix, isdepth);
|
||||
}
|
||||
} else {
|
||||
String filePath = f.getAbsolutePath();
|
||||
|
||||
if (suffix != null) {
|
||||
int begIndex = filePath.lastIndexOf(".");// 最后一个.(即后缀名前面的.)的索引
|
||||
String tempsuffix = "";
|
||||
|
||||
if (begIndex != -1)// 防止是文件但却没有后缀名结束的文件
|
||||
{
|
||||
tempsuffix = filePath.substring(begIndex + 1, filePath
|
||||
.length());
|
||||
}
|
||||
|
||||
if (tempsuffix.equals(suffix)) {
|
||||
fileList.add(filePath);
|
||||
}
|
||||
} else {
|
||||
// 后缀名为null则为所有文件
|
||||
fileList.add(filePath);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return fileList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法追加文件:使用FileWriter
|
||||
*
|
||||
* @param fileName
|
||||
* @param content
|
||||
*/
|
||||
public static void appendMethod(String fileName, String content) {
|
||||
try {
|
||||
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
|
||||
FileWriter writer = new FileWriter(fileName, true);
|
||||
writer.write(content + "\r\n");
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user