1,指定文件完整路径名的写读
public void FilereadBybyte(String path) throws IOException { FileInputStream file = new FileInputStream(path); int n = 0; byte[] buffer = new byte[512]; while ((n = file.read(buffer)) != -1) { System.out.write(buffer, 0, n); } file.close(); } public void FilewriteBybyte(String path) throws IOException { FileOutputStream fos = new FileOutputStream(path, true); String s = "面朝大海,春暧花开;\n"; byte[] bytes = s.getBytes(); fos.write(bytes, 0, bytes.length); fos.close(); } public void FilecopyBybyte(String path, String path2) throws IOException { FileInputStream fil = new FileInputStream(path); FileOutputStream file = new FileOutputStream(path2); byte[] buffer = new byte[1]; int n = 0; while ((n = fil.read(buffer)) != -1) { file.write(buffer, 0, n); file.flush();// 强行把文件字符刷新出去 } fil.close(); file.close(); }
void FilereadByString(String path) throws IOException { FileReader fr = new FileReader(path); BufferedReader br = new BufferedReader(fr); String s = null; while ((s = br.readLine()) != null) { System.out.println(s); } fr.close(); } void FilewriteByString(String path) throws IOException { FileWriter fw = new FileWriter(path); BufferedWriter bw = new BufferedWriter(fw); bw.write("面朝大海,春暧花开;"); bw.newLine(); bw.write(" 海子! "); bw.newLine(); bw.close(); } void FilecopyByString(String src, String des) throws IOException { FileReader fr = new FileReader(src); BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter(des); BufferedWriter bw = new BufferedWriter(fw); String s = null; while ((s = br.readLine()) != null) { bw.newLine(); fw.write(s); } fr.close(); fw.close(); }
public static String read(File src) { StringBuffer res = new StringBuffer(); String line = null; try { BufferedReader reader = new BufferedReader(new FileReader(src)); while ((line = reader.readLine()) != null) { res.append(line + "\n"); } reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return res.toString(); }
说明: 写道
以字节流读写,可以操作所有文件,但按行读取只能操作相应该的文本文件,
此外:字节流写入文件是将之内容写入指定文件内容最后面,而按行读取写入的数据会比之前的内容清空,再重新写入...
此外:字节流写入文件是将之内容写入指定文件内容最后面,而按行读取写入的数据会比之前的内容清空,再重新写入...
private boolean saveOrUpdateEmail(EmailNotesBO enBO) {
String fileName=enBO.getFile().getOriginalFilename();
if(!(fileName==null && "".equals(fileName))) {
List fileTypes = new ArrayList();
fileTypes.add("jpg");
fileTypes.add("xlsx");
fileTypes.add("txt");
fileTypes.add("docx");
fileTypes.add("doc");
fileTypes.add("xls");
String ext = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
ext = ext.toLowerCase();
if(fileTypes.contains(ext)) {
contactService.saveOutboundEmail(enBO);
}
return true;
}else{
logger.info("**** the file,"+fileName+" upload error ! ****");
return false;
}
}