Java文件读写(IO)常用代码(一)

2014-11-24 02:01:34 · 作者: · 浏览: 4

/**
* 创建一个新的文件
* @param relativePath 相对路径
* @param fileName 文件名
* @return
* @throws IOException
*/
public File createFile(String relativePath, String fileName) throws IOException {

String upPath = getFileUploadPath() + relativePath + "//";
System.out.println("upPath:" + upPath);/////////////////////////////
System.out.println("fileName:" + fileName);/////////////////////////////
String nameWithoutExt = getNameWithoutExtension(fileName);
String ext = getExtension(fileName);

createDirectory(upPath);// 上传目录不存在则创建目录
File newFile = new File(upPath + fileName);

// 若有重名文件则修改名称创建新的文件
for (int counter = 1; newFile.exists(); counter ++) {
fileName = nameWithoutExt + "(" + String.valueOf(counter) + ")." + ext;
newFile = new File(upPath + fileName);
}
newFile.createNewFile();
return newFile;
}

=================================================================================

/**
* 文件的写入 (单行写入)
* @param filePath(文件路径)
* @param fileName(文件名)
* @param args
* @throws IOException
*/
public void writeFile(String filePath,String fileName,String args) throws IOException
{
FileWriter fw = new FileWriter(filePath+fileName);
fw.write(args);
fw.close();
}

=================================================================================

/**
* 文件读写
*
* @author 贺圆波
* 2009-10-26
* @param fileName
* @return void
*/
public void addLogger(String fileName) {
String line = "";
File file = new File("E://tkmFile//knlgfile//template//template.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
while (reader.read() != -1) {
line += reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
reader = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
line = " " + line.substring(0, line.lastIndexOf("]"));
line += ",[/"750/", /"" + fileName + "/", /"" + fileName + "/"]" + "];";
// System.out.println(line);////////////////////////////
FileWriter fw = null;
try {
fw = new FileWriter(Constants.templatePath);
fw.write(line);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fw != null) {
fw.close();
fw = null;
}
} catch (IOException e) {
e.printStackTrace();
}

}
}

=================================================================================

====================================================================================

/**
* 文件的写入(按数组逐行写入)
* @param filePath(文件路径)
* @param fileName(文件名)
* @param args[]
* @throws IOException
*/
public void writeFile(String filePath,String fileName,String[] args) throws IOException
{
FileWriter fw = new FileWriter(filePath+fileName);
PrintWriter out=new PrintWriter(fw);
for(int i=0;i {
out.write(args[i]);
out.println();
out.flush();
}
fw.close();
out.close();
}

===================================================================================

/**
* 生成Excel文件发送给用户
* @author he 2009-10-20
* @param response - HttpervletResponse 对象
* @param sheetName - 表名
* @param titles - 标题数组
* @param contents - 内容集合,包含多个内容的String数组
* @throws Exception
*/
public static void writeExcel(HttpServletResponse response, String sheetName, String titles[], List contents,String filename) throws Exception {

response.setHeader("Content-disposition","attachment; filename="+filename+".xls");
response.setContentType("application/mse