读取写入文件内容
2012-12-05 17:54
281 查看
读取文件内容
读取文件内容2
写入文件
private String getEmailBody(String lang, String mobile, String refnumber){ ClassLoader cl; String str = ""; InputStream is = null; BufferedReader in = null; try{ cl = Thread.currentThread().getContextClassLoader(); if (lang == null){ lang="chi"; } if (lang.equals("chi")){ is = cl.getResourceAsStream("com/mmt/mail/mailBody.html"); }else{ is = cl.getResourceAsStream("com/mmt/mail/mailBody_en.html"); } in = new BufferedReader(new InputStreamReader(is, "UTF-8")); String temp; while ((temp = in.readLine()) != null) { str += temp + "\r\n"; } str = str.replaceAll("%%mobile%%", mobile).replaceAll("%%refnumber%%", refnumber); str = str.trim(); }catch(Exception e){ e.printStackTrace(); }finally{ try{in.close();in=null;}catch(Exception e){} try{is.close();is=null;}catch(Exception e){} cl=null; } return str; }
读取文件内容2
/** * 以行为单位读取文件,常用于读面向行的格式化文件 */ public static boolean readFileByLines(File file) { boolean b = false; BufferedReader reader = null; FileInputStream fis = null; InputStreamReader isr = null; try { List<List<Object>> marList = new ArrayList<List<Object>>(); // System.out.println("以行为单位读取文件内容,一次读一整行:"); String tempString = null; // String html = ""; int line = 1; fis = new FileInputStream( file ); isr = new InputStreamReader(fis, "UTF-8" ); reader = new BufferedReader(isr); // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { // 显示行号 // System.out.println("line " + line + ": " + tempString); if (line > 2) { String actual_on_air_date = tempString.substring(1, 9); String hard_start_flag = tempString.substring(9, 10); String actual_on_air_time = tempString.substring(10, 21); String material_id = tempString.substring(22, 54); String segment_number = tempString.substring(55, 57); String title = tempString.substring(58, 90); String actual_duration = tempString.substring(91, 102); String status = tempString.substring(103, 110); String on_air_device = tempString.substring(111, 119); String channel = tempString.substring(120, 123); String event_type = tempString.substring(123, 130); String secondary_type = tempString.substring(131, 135); // System.out.println(actual_on_air_date); // System.out.println(hard_start_flag); // System.out.println(actual_on_air_time); // System.out.println(material_id); // System.out.println(segment_number); // System.out.println(title); // System.out.println(actual_duration); // System.out.println(status); // System.out.println(on_air_device); // System.out.println(channel); // System.out.println(event_type); // System.out.println(secondary_type); List<Object> list = new ArrayList<Object>(); list.add(actual_on_air_date); list.add(hard_start_flag); list.add(actual_on_air_time); list.add(material_id); list.add(segment_number); list.add(title); list.add(actual_duration); list.add(status); list.add(on_air_device); list.add(channel); list.add(event_type); list.add(secondary_type); marList.add(list); } // html += tempString; line++; } reader.close(); if(marList.size() > 0){ AsRunService as = new AsRunService(); b = as.addAsRunLogBatch(marList); if (b) { System.out.println("add asrun log success..."); }else{ System.out.println("add asrun log failure..."); } } // BufferedWriter output = new BufferedWriter(new FileWriter(new File(PropertiesUtils.getProperties("asrunbak")+"/123.txt"))); // output.write(html); // output.close(); } catch (IOException e) { e.printStackTrace(); b = false; } finally { if (reader != null) { try {reader.close();} catch (IOException e1) { } try {isr.close();} catch (IOException e1) { } try {fis.close();} catch (IOException e1) { } } } return b; }
写入文件
/** * 文件保存 * * @param file * 要保存的文件 * @param path * 保存的文件路径 * @param fileName * 文件名称 * @return boolean */ public static boolean saveFile(File file, String path, String fileName) { boolean b = false; try { String root = ServletActionContext.getRequest().getRealPath("/") + PropertiesUtils.getProperties("oao_media_path")+path; // String root = PropertiesUtils.getProperties("oao_media_path")+path; System.out.println(root); File f = new File(path); if (!f.isDirectory()) { f.mkdirs(); } InputStream is = new FileInputStream(file); File destFile = new File(root, fileName); OutputStream os = new FileOutputStream(destFile); byte[] buffer = new byte[1025]; int length = 0; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } is.close(); os.close(); b = true; } catch (Exception e) { b = false; // TODO Auto-generated catch block e.printStackTrace(); } return b; }
/** * * 用FileWrite向文件写入内容 * * * * @param _destFile * * @throws IOException */ public static void writeByFileWrite(String _sDestFile, String _sContent) throws IOException { FileWriter fw = null; try { fw = new FileWriter(_sDestFile); fw.write(_sContent); } catch (Exception ex) { ex.printStackTrace(); } finally { if (fw != null) { fw.close(); fw = null; } } } /** * * 用FileOutputStream向文件写入内容 * * * * @param _destFile * * @throws IOException */ public static void writeByFileOutputStream(String _sDestFile, String _sContent) throws IOException { FileOutputStream fos = null; try { fos = new FileOutputStream(_sDestFile); fos.write(_sContent.getBytes()); } catch (Exception ex) { ex.printStackTrace(); } finally { if (fos != null) { fos.close(); fos = null; } } } /** * * 用OutputStreamWrite向文件写入内容 * * * * @param _destFile * * @throws IOException */ public static void writeByOutputStreamWrite(String _sDestFile, String _sContent) throws IOException { OutputStreamWriter os = null; FileOutputStream fos = null; try { fos = new FileOutputStream(_sDestFile); os = new OutputStreamWriter(fos, "UTF-8"); os.write(_sContent); } catch (Exception ex) { ex.printStackTrace(); } finally { if (os != null) { os.close(); os = null; } if (fos != null) { fos.close(); fos = null; } } }
相关文章推荐
- 从文件读取内容,删除一个数据,把删除后的数据重新写入到文件
- JAVA 创建TXT文件,写入文件内容,读取文件内容
- JAVA 创建TXT文件,写入文件内容,读取文件内容
- java按指定编码写入和读取文件内容的类
- API读取写入 ini文件内容的方法函数详解
- 读取()txt文件中的内容与向txt文件中写入内容(覆盖重写与在末尾续写+FileOutputStream与FileWriter)
- 文件读取,写入(去除源文件内容)以及追加写入(保留原文件内容)
- API读取写入 ini文件内容的方法函数详解 【转】
- sql 读取txt 文件内容,并写入sql的方法
- 使用FileReader和FileWriter读取写入文件内容
- C语言逐行读取文件内容,写入另外一个文件
- java按指定编码写入和读取文件内容的类
- API读取写入 ini文件内容的方法函数详解
- 使用FileReader和FileWriter读取写入文件内容
- JAVA 创建TXT文件,写入文件内容,读取文件内容
- JAVA 创建TXT文件,写入文件内容,读取文件内容
- Java将字符串写入文件与将文件内容读取到字符串
- java 创建文件 写入内容 读取内容
- 通过字符流来对文件进行内容的读取和写入操作
- PHP 读取文本文件内容写入数据库