首页 > 代码库 > 自动化测试框架-----公用类
自动化测试框架-----公用类
其中文件公用类如下:
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; public class FileUtils { /** * 功能:向指定的文件中写入字符串(指定是否覆盖) * @param outputFile 要写入的文件 * @param sb 要写入的字符串 * @param encoding 字符编码 * @param append 是否要追加或者覆盖原有内容 * */ public static void writeFile(File outputFile, String sb, String encoding, boolean append) { BufferedWriter fw = null; try { if (!(outputFile.exists())) outputFile.createNewFile(); OutputStreamWriter osw = null; if (null != encoding) { osw = new OutputStreamWriter(new FileOutputStream(outputFile, append), encoding); } else osw = new OutputStreamWriter(new FileOutputStream(outputFile, append)); fw = new BufferedWriter(osw); fw.write(sb); } catch (IOException ex) { ex.printStackTrace(); } finally { try { if (fw != null) fw.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 功能:得到文件的内容 * @param is 读入流(InputStream) * @return String 返回的字符串 * * */ public static String streamToString(InputStream is) throws IOException { if (is != null) { Writer writer = new StringWriter(); char[] buffer = new char[1024]; try { Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } } finally { is.close(); } return writer.toString(); } return ""; } /** * 功能:向指定的文件中写入字符串(指定是否覆盖) * @param f 要写入的文件 * @param string 要写入的字符串 * @throws IOException * */ public static void writeFile(String string,File f) throws IOException { FileWriter writer=new FileWriter(f); BufferedWriter bw = new BufferedWriter(writer); bw.write(string); bw.close(); writer.close(); } /** * 功能:读文件 * @param InputStream 要写入的文件 * @return string 返回的字符串 * @throws IOException * */ public static String readFile(InputStream in) throws Exception{ StringBuilder builder=new StringBuilder(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line=br.readLine(); while (line != null) { builder.append(line + "\n"); line = br.readLine(); } return builder.toString(); } }
自动化测试框架-----公用类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。