[java]代码库
/**
* @param lineNumber 选择要添加内容的行数
* @param attribute1 选择要添加的内容1
* @param attribute2 选择要添加的内容1
* 给finalFile,tempFilePath改成你自己的路径就可以用了
*/
public static void appendText(int lineNumber, String attribute1, String attribute2) throws IOException {
int count = 0;
String tempFilePath=NewPath.sampleFolderPath+"\\temp.java";
File finalFile = new File(NewPath.sample_Main);
File tempFile=new File(tempFilePath);
Utils.CopyFile(NewPath.sample_Main, tempFilePath);
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(tempFile);
//读取文件类
InputStreamReader inReader = new InputStreamReader(fileInputStream, "UTF-8");
BufferedReader bufReader = new BufferedReader(inReader);
String line = null;
//写入文件类
OutputStreamWriter fileWriter = new OutputStreamWriter(new FileOutputStream(finalFile),"UTF-8");
BufferedWriter bufWriter=new BufferedWriter(fileWriter);
//边读边写,并添加内容
while ((line = bufReader.readLine()) != null) {
count++;
bufWriter.write(line);
bufWriter.newLine();
if (count == lineNumber) {
bufWriter.append(" item = new HashMap<String, Object>();");
bufWriter.newLine();
bufWriter.append(" item.put(\"activity_name\", \"" + attribute1 + "\");");
bufWriter.newLine();
bufWriter.append(" item.put(\"activity_class\", " + attribute2 + ");");
bufWriter.newLine();
bufWriter.append(" listItems.add(item);");
bufWriter.newLine();
bufWriter.newLine();
}
}
//关闭输入输出流
bufWriter.close();
fileWriter.close();
bufReader.close();
inReader.close();
fileInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.gc();
tempFile.delete();
}