Java处理UTF-8带BOM的文本的读写(五)

2014-11-24 01:45:17 · 作者: · 浏览: 2
return generateQTPScript(target.getAbsolutePath(), source

.getAbsolutePath());

}

/**

* Generate QTP Script

*

* @param target

* target file path

* @param source

* source file path

* @return

* @throws IOException

*/

public File generateQTPScript(String target, String source)

throws IOException {

File f = new File(target);

if (!f.exists()) {

f.getParentFile().mkdirs();

try {

f.createNewFile();

} catch (Exception e) {

}

}

FileOutputStream fos = null;

OutputStreamWriter osw = null;

BufferedWriter bw = null;

try {

final byte[] bom = new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF };

fos = new FileOutputStream(f);

osw = new OutputStreamWriter(fos, ENCODING);

bw = new BufferedWriter(osw);

fos.write(bom);

bw.write(scriptConvey(getSourceFileContentReader(source)));

bw.flush();

bw.close();

return f;

} catch (IOException e) {

throw e;

}

}

/**

* Reader convey to string

*

* @param source

* @return

* @throws IOException

*/

private String reader2String(Reader source) throws IOException {

BufferedReader bufferedReader = new BufferedReader(source);

StringBuffer result = new StringBuffer();

String buffer = null;

while ((buffer = bufferedReader.readLine()) != null) {

result.append(buffer + "\n");

}

return result.toString();

}

/**

*

* @param source

* file path

* @return

* @throws IOException

*/

private Reader getReader(String source) throws IOException {

return source == "" null : new BufferedReader(new InputStreamReader(

getInputStream(source)));

}

/**

* get script file content string

*

* @param source

* @return

* @throws IOException

*/

private String getSourceFileContentReader(String source) throws IOException {

return source == "" "" : reader2String(getReader(source));

}

/**

* get inputstream

*

* @param source

* file path

* @return

* @throws IOException

*/

private InputStream getInputStream(String source) throws IOException {

return source == "" null : new FileInputStream(new File(source));

}

/**

* Replace all occurences of a substring within a string with another

* string.

*

* @param inString

* String to examine

* @param oldPattern

* String to replace

* @param newPattern

* String to insert

* @return a String with the replacements

*/

private String replace(String inString, String oldPattern, String newPattern) {

if (!hasLength(inString) || !hasLength(oldPattern)

|| newPattern == null) {

return inString;

}

StringBuilder sb = new StringBuilder();

int pos = 0;

int index = inString.indexOf(oldPattern);