Tag: 追加

如何在log4j2中创建滚动文件appender插件

我想创建一个自定义log4j2滚动文件appender。 我需要创建这个自定义appender,因为我想用我的应用程序特有的一些信息包装log4j日志事件。 例如userId,托管的应用程序名称。 我有一个扩展Log4jLogEvent实现LogEvent的类。 这个类包含了我需要用日志事件包装的信息。 请看这段代码: public class CustomLogEvent extends Log4jLogEvent implements LogEvent { private String userId; private String applicationName; private static final long serialVersionUID = 1L; public CustomLogEvent(String loggerName, Marker marker, String loggerFQCN, Level level, Message message, Throwable t, Map mdc, ThreadContext.ContextStack ndc, String threadName, StackTraceElement location, long timestamp){ super(loggerName,marker,loggerFQCN,level,message,t,mdc,ndc,threadName,location,timestamp); } //Getters and setters […]

如何在不覆盖现有数据的情况下使用DOM附加现有XML文件? 在java中

我对如何将xml数据附加到已存在的数据非常困惑,请给我你的建议我的代码是这样的 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();//db.newDocument();//create document Element root = doc.createElement(“Employees”);//cretae Elements doc.appendChild(root); Comment cmt = doc.createComment(“Employee Details”);//Add comment to xml root.appendChild(cmt); Element employee = doc.createElement(“employee”);//create Element //employee.appendChild(doc.) root.appendChild(employee); Attr genderAttr = doc.createAttribute(“Gender”); System.out.print(“Enter your gender :”); String gend = br.readLine(); genderAttr.setValue(gend); employee.setAttributeNode(genderAttr); System.out.print(“Enter first name:”); String child = br.readLine(); Element FName = doc.createElement(“firstName”); FName.appendChild(doc.createTextNode(child));//set xml […]

JTextField的“附加”文本

我正在尝试创建一个小的应用程序,其中U输入ID(在JButton的帮助下 – 从0到9 – )然后将已经按下的数字传递给putText-Method然后将其显示在JTextField中 – 问题在于每一个时间我按下我之前按下的那个新数字:s。 有人可以帮助我吗? public class IdPanel extends JPanel { private JTextField idField; private JLabel idLabel; public IdPanel() { setLayout(new GridBagLayout()); setPreferredSize(new Dimension(500, 70)); idField = new JTextField(20); idField.setFont(new Font(“Serif”, Font.PLAIN, 20)); idLabel = new JLabel(“Enter ID:”); idLabel.setFont(new Font(“Serif”, Font.PLAIN, 20)); GridBagConstraints gc = new GridBagConstraints(); gc.gridx = 0; gc.gridy = […]

可以将apache FileUtils.writeLines()附加到文件(如果存在)

公共文件FileUtils看起来很酷,我无法相信它们不能被附加到文件中。 File file = new File(path); FileUtils.writeLines(file, printStats(new DateTime(), headerRequired)); 以上只是每次都替换文件的内容,我只想继续标记这些东西,就像这段代码一样。 fw = new FileWriter(file, true); try{ for(String line : printStats(new DateTime(), headerRequired)){ fw.write(line + “\n”); } } finally{ fw.close(); } 我搜索了javadoc,却一无所获! 我错过了什么?

hadoop2.2.0追加文件发生AlreadyBeingCreatedException

我遇到了关于hadoop2.2.0追加操作的问题。 我通过HDFS java API将一些字节附加到hdfs文件。首先,如果在附加操作之前文件不存在,我将创建目标文件,代码如下: String fileUri = “hdfs://hadoopmaster:9000/in/append_test.txt”; // create the hdfs file, if not exists HdfsClient.createPathIfNotExist(fileUri); // do 5 times append operation for (int i=0; i<5; i++){ HdfsClient.appendTo(fileUri, ("append content"+i).getBytes("UTF-8")); } createPathIfNotExist函数: Path p = null; FileSystem fs = null; try { fs = FileSystem.get(URI.create(uri), conf); p = new Path(uri); if (!fs.exists(p)) { if […]

StringBuilder追加()和null值

我有一个String列表,我想在它们之间用空格连接它们。 所以我正在使用StringBuilder 。 现在,如果任何String为null ,则它们在String中存储在StringBuilder为“null”。 这是一个小程序来说明问题: public static void main(String ss[]) { StringBuilder sb = new StringBuilder(); String s; s = null; System.out.println(sb.append(“Value: “).append(s)); } 我希望输出为“Value:”但它出现为“Value:null” 有没有解决这个问题的方法?

如何在Matcher组上追加替换而不是整个模式?

我正在使用while(matcher.find())循环遍历Pattern的所有匹配项。 对于它找到的那个模式的每个实例或匹配,我想用一些新文本替换matcher.group(3) 。 这个文本对于每个文本都是不同的,所以我使用matcher.appendReplacement()来重建原始字符串,并使用新的更改。 但是, appendReplacement()替换整个Pattern,而不仅仅是组。 我该怎么做但只修改匹配的第三组而不是整个模式? 这是一些示例代码: Pattern pattern = Pattern.compile(“THE (REGEX) (EXPRESSION) (WITH MULTIPLE) GROUPS”); Matcher matcher = pattern.matcher(“THE TEXT TO SEARCH AND MODIFY”); StringBuffer buffer = new StringBuffer(); while(matcher.find()){ matcher.appendReplacement(buffer, processTheGroup(matcher.group(3)); } 但我想做这样的事情(显然这不起作用)。 … while(matcher.find()){ matcher.group(3).appendReplacement(buffer, processTheGroup(matcher.group(3)); } 像这样的东西,它只取代某个组,而不是整个模式。 编辑:更改正则表达式示例以显示并非所有模式都已分组。

将数组输入到GUI Jtextarea中

一直试图将我的arrays,房子的年份和大小添加到GUI中,更准确地说是JTextarea。 这样做最简单的方法是什么? 还没有完全掌握数据输入。 public class House { private int year; private int size; private static int nbrOfHouses; public static final int MIN_SIZE = 10; public House(int year, int size) { this.year = year; this.size = size; } public static int getNbrHouses() { return nbrOfHouses; } public int getYear() { return year; } public int getSize() […]

何时首选`StringBuilder`而不是将`String`附加到`String`?

以下是如何追加String两种方法: String firstString = “text_0”; String secondString = “text_1”; String resultString = firstString + secondString; StringBuilder sb = new StringBuilder(); sb.append(firstString).append(secondString); String resultString = sb.toString(); 我的问题是 – 什么时候使用StringBuilder更有效? 假设有10个字符串,我需要创建其中一个字符串。

如何在数组末尾添加元素?

我想知道如何在数组的末尾添加或追加一个新元素。 有没有简单的方法在最后添加元素? 我知道如何使用StringBuffer,但我不知道如何使用它来添加数组中的元素。 没有ArrayList或列表我更喜欢它。 我想知道StringBuffer是否可以处理整数。