java文件读写

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;
     	}			  
	}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值