下面列出了java.io.FileWriter#append ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
String compile() throws IOException {
// Create a class that references classId
File explicit = new File("ExplicitClass.java");
FileWriter filewriter = new FileWriter(explicit);
filewriter.append("class ExplicitClass { " + classId + " implicit; }");
filewriter.close();
StringWriter sw = new StringWriter();
com.sun.tools.javac.Main.compile(new String[] {
"-verbose",
option.optionString,
"-source", "8", "-target", "8",
"-sourcepath", Dir.SOURCE_PATH.file.getPath(),
"-classpath", Dir.CLASS_PATH.file.getPath(),
"-Xbootclasspath/p:" + Dir.BOOT_PATH.file.getPath(),
"-d", XPreferTest.OUTPUT_DIR.getPath(),
explicit.getPath()
}, new PrintWriter(sw));
return sw.toString();
}
private static void dumpTable(String name,FileWriter sb) throws IOException{
String query="SELECT * FROM "+ name;
Cursor c=db.rawQuery(query,null);
sb.write("DUMPING: ");
sb.write(name);
sb.write(" count: ");
sb.write(""+c.getCount());
sb.write(": ");
if(c.moveToFirst()){
do{
sb.write(DatabaseUtils.dumpCurrentRowToString(c));
}while(c.moveToNext());
}
c.close();
sb.append("END DUMPING\n");
}
private static void genericCvResultsFileWriter(String outFilePathAndName, Instances instances, double[] preds, String datasetName, String classifierName, String paramInfo, double cvAcc) throws Exception{
if(instances.numInstances()!=preds.length){
throw new Exception("Error: num instances doesn't match num preds.");
}
File outPath = new File(outFilePathAndName);
outPath.getParentFile().mkdirs();
FileWriter out = new FileWriter(outFilePathAndName);
out.append(datasetName+","+classifierName+",train\n");
out.append(paramInfo+"\n");
out.append(cvAcc+"\n");
for(int i =0; i < instances.numInstances(); i++){
out.append(instances.instance(i).classValue()+","+preds[i]+"\n");
}
out.close();
}
protected void createCMISParametersFile() throws IOException
{
File f = File.createTempFile("OpenCMISTCKContext", "" + System.currentTimeMillis(), new File(System.getProperty("java.io.tmpdir")));
f.deleteOnExit();
FileWriter fw = new FileWriter(f);
for(String key : cmisParameters.keySet())
{
fw.append(key);
fw.append("=");
fw.append(cmisParameters.get(key));
fw.append("\n");
}
fw.close();
System.setProperty(JUnitHelper.JUNIT_PARAMETERS, f.getAbsolutePath());
System.out.println("CMIS client parameters file: " + f.getAbsolutePath());
}
/**
* Main method. When args.length > 0, clusterMaster method is triggered with
* args instead of the local main method.
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception{
if(args.length>0){
clusterMaster(args);
return;
}
// else, local:
// String problemName = "alphabet_raw_26_sampled_10";
String problemName = "vowel_raw_sampled_10";
StringBuilder instructionBuilder = new StringBuilder();
for(ElasticEnsemble.ConstituentClassifiers c:ElasticEnsemble.ConstituentClassifiers.values()){
scriptMaker_runCv(problemName, 0, c, instructionBuilder);
}
FileWriter out = new FileWriter("instructions_"+problemName+".txt");
out.append(instructionBuilder);
out.close();
}
/**
* Test of getKeyCount method, of class MultiBundleStructure.
*/
@Test
public void testGetKeyCount() throws Exception {
System.out.println("getKeyCount");
File propFile = new File(getWorkDir(), "foo.properties");
propFile.createNewFile();
FileWriter wr = new FileWriter(propFile);
wr.append("a=1\nb=2");
wr.close();
DataObject propDO = DataObject.find(FileUtil.toFileObject(propFile));
assertTrue(propDO instanceof PropertiesDataObject);
PropertiesDataObject dataObject = (PropertiesDataObject) propDO;
MultiBundleStructure instance = new MultiBundleStructure(dataObject);
instance.updateEntries();
int expResult = 2;
int result = instance.getKeyCount();
assertEquals(expResult, result);
}
/**
* Appends the given data to the file specified in the input and returns the
* reference to the file.
*
* @param dataToAppend
* @return reference to the file.
*/
public static File appendDataToTempFile(File file, String dataToAppend)
throws IOException {
FileWriter outputWriter = new FileWriter(file);
try {
outputWriter.append(dataToAppend);
} finally {
outputWriter.close();
}
return file;
}
private static void write(FileWriter fw, List<String> tokens) throws IOException {
for (String token : tokens) {
fw.append(token);
fw.append(" ");
}
fw.append(Vocabulary.EOS);
fw.append("\n");
}
private void writeRecordInfoCsvFile(FileWriter writer, ResultSet resultSet,
int columnCount) throws SQLException, IOException {
for (int i = 1; i <= columnCount; i++) {
Object temp = resultSet.getObject(i);
if (temp != null) {
writer.append(temp.toString());
}
writer.append( VALUES_SEPARATOR );
}
writer.append("\n");
}
/**
* 将日志信息写到本地文件中
*
* @param tag 标签,也是存放日志的文件夹名
* @param msg 内容
*/
private static void writeToFile(String tag, String msg) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
try {
String dir = mPath + tag + "/";
Date now = new Date();
SimpleDateFormat tempDate1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
SimpleDateFormat tempDate2 = new SimpleDateFormat("yyyyMMdd", Locale.CHINA);
String dateTime = tempDate1.format(now);
String fileName = tempDate2.format(now);
File destDir = new File(dir);
if (!destDir.exists()) {
destDir.mkdirs();
}
File file = new File(dir + fileName + ".txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file, true);
fw.append("\r\n=============" + dateTime + "=====================\r\n");
fw.append(msg);
fw.flush();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void writeDebugInfo(File out) {
try {
FileWriter writer = new FileWriter(out);
writer.append(deviceInfo.toString());
writer.close();
} catch (IOException e) {
}
}
public static void printIntensityTimeGraphInCSVFormat(List<XZ> values, String fileName) throws Exception {
FileWriter chartWriter = new FileWriter(fileName);
chartWriter.append("Intensity, Time");
chartWriter.append(NEW_LINE_SEPARATOR);
for (XZ point : values) {
chartWriter.append(point.getIntensity().toString());
chartWriter.append(COMMA_DELIMITER);
chartWriter.append(point.getTime().toString());
chartWriter.append(NEW_LINE_SEPARATOR);
}
chartWriter.flush();
chartWriter.close();
}
public synchronized void addToReport(MSSQLTestData td, Object result) {
System.out.println("called");
try {
FileWriter fr = new FileWriter(getResportFileName(), true);
String offset = td.getData(KEY_STRINGS.OFFSET);
String res = "_";
if (result == null) {
res = "Success";
} else {
try {
res = "FAILED "
+ removeNewLines(((AssertionError) result)
.getMessage());
} catch (Exception ae) {
if (result instanceof Exception
&& ((Exception) result) != null) {
res = "FAILED "
+ removeNewLines(((Exception) result)
.getMessage());
} else {
res = "FAILED " + result.toString();
}
}
}
fr.append(offset + "\t" + res + "\n");
fr.close();
} catch (Exception e) {
LOG.error(StringUtils.stringifyException(e));
}
}
private void assertContents(StringBuilder sb) throws IOException {
File out = new File(getWorkDir(), fname + ".parsed");
FileWriter wr = new FileWriter(out);
wr.append(sb);
wr.close();
assertFile(out, getGoldenFile(fname + ".pass"), new File(getWorkDir(), fname + ".diff"));
}
private synchronized void writeCommandInLogFile(String command){
File logFile = new File(this.logFileName);
String line="\n\n************************************ Tickler: Executing Command ************************************************\n"
+command+"\n *******************************************************************************************************************\n\n";
try{
FileWriter w = new FileWriter(logFile,true);
w.append(line);
w.close();
}
catch (IOException e){
e.printStackTrace();
}
}
/**
* Appends the given data to the file specified in the input and returns the
* reference to the file.
*
* @param file
* @param dataToAppend
* @return reference to the file.
* @throws IOException
*/
public static File appendDataToTempFile(File file, String dataToAppend)
throws IOException {
FileWriter outputWriter = new FileWriter(file);
try {
outputWriter.append(dataToAppend);
} finally {
outputWriter.close();
}
return file;
}
public void serialize(String path, String tab, PrintWriter pw, File baseFolder) throws IOException {
if (!getName().isEmpty()) {
String n = getName();
if (isLeaf()) {
pw.print(tab+"<file name=\""+n + "\"");
} else {
pw.print(tab+"<folder name=\""+n + "\"");
}
String urlVal = null;
if (fileContents != null) {
urlVal = (path + getName()).replaceAll("/", "-");
File f = new File(baseFolder, urlVal);
FileWriter wr = new FileWriter(f);
wr.append(fileContents);
wr.close();
} else if (contentURL != null) {
urlVal = contentURL.toExternalForm();
}
if (urlVal != null) {
pw.print(" url=\"" + urlVal + "\"");
}
pw.print(">");
for (String s : attributeContents.keySet()) {
pw.print("\n" + tab + " <attr name=\"" + s + "\" " +
attributeTypes.get(s) + "=\"" + attributeContents.get(s) + "\"/>");
}
pw.println();
}
String newPath = path + getName();
if (!newPath.isEmpty()) {
newPath = newPath + "/";
}
for (ResourceElement res : children.values()) {
res.serialize(newPath, tab + " ", pw, baseFolder);
}
if (!getName().isEmpty()) {
if (isLeaf()) {
pw.println(tab+"</file>" );
} else {
pw.println(tab+"</folder>" );
}
}
}
/**
* Dump merging tool actions to a text file.
* @param fileWriter the file to write all actions into.
* @throws IOException
*/
void log(FileWriter fileWriter) throws IOException {
fileWriter.append(getLogs());
}
/**
* This is always called on a single background thread.
* Implementing classes must ONLY write to the fileWriter and nothing more.
* The abstract class takes care of everything else including close the stream and catching IOException
*
* @param fileWriter an instance of FileWriter already initialised to the correct file
*/
private void writeLog(FileWriter fileWriter, String content) throws IOException {
fileWriter.append(content);
}
/**
* Dump merging tool actions to a text file.
* @param fileWriter the file to write all actions into.
* @throws IOException
*/
void log(FileWriter fileWriter) throws IOException {
fileWriter.append(getLogs());
}