在Java中现有数据之间的文本文件中添加数据

在Java中现有数据之间的文本文件中添加数据,java,edit,javaparser,Java,Edit,Javaparser,我需要在类名或方法名之前添加一个类或一个text/java文件中的方法(行号可通过japa获得) 我已经尝试了所有可能的情况,尝试使用文件读取器/写入器编辑文本/Java文件(但它总是在末尾追加,并且需要以上的类/方法名称)、pmd、japa(可以读取但找不到编辑选项)和随机访问文件接口(也可以编辑,但不追加字符) 到目前为止没有任何帮助,我有没有办法做到这一点 例如,原始源代码: import java.lang.*; class test1{ public static void m

我需要在类名或方法名之前添加一个类或一个text/java文件中的方法(行号可通过japa获得)

我已经尝试了所有可能的情况,尝试使用文件读取器/写入器编辑文本/Java文件(但它总是在末尾追加,并且需要以上的类/方法名称)、pmd、japa(可以读取但找不到编辑选项)和随机访问文件接口(也可以编辑,但不追加字符)

到目前为止没有任何帮助,我有没有办法做到这一点

例如,原始源代码:

import java.lang.*;
class test1{
    public static void main(String[] args) {
        System.out.println("hello world");
    }
}
之后

import java.lang.*;
/*this is a class*/
class test1{
    /*this is a method*/
    public static void main(String[] args) {
        System.out.println("hello world");
    }
}

您可以使用JavaParser实现自己的
ModifierVisitorAdapter
。下面是一个小片段,你可以从中开始

import japa.parser.JavaParser;
import japa.parser.ParseException;
import japa.parser.ast.CompilationUnit;
import japa.parser.ast.Node;
import japa.parser.ast.body.ClassOrInterfaceDeclaration;
import japa.parser.ast.body.MethodDeclaration;
import japa.parser.ast.comments.BlockComment;
import japa.parser.ast.visitor.ModifierVisitorAdapter;
import java.io.File;
import java.io.IOException;

public class ModifierVisitorAdapterDemo {

    public static void main(String[] args) throws IOException, ParseException {
        File sourceFile = new File("Test.java");
        CompilationUnit cu = JavaParser.parse(sourceFile);
        cu.accept(new ModifierVisitor(), null);

        // instead of printing it to stdout you can write it to a file
        System.out.println("// enriched cource");
        System.out.println(cu.toString());

        // to save it into the original file
        // this will overwrite the input file
        Files.write(sourceFile.toPath(), 
                cu.toString().getBytes(Charset.defaultCharset()), 
                StandardOpenOption.TRUNCATE_EXISTING
        );
    }

    private static class ModifierVisitor extends ModifierVisitorAdapter {

        @Override
        public Node visit(ClassOrInterfaceDeclaration n, Object arg) {
            if (!n.isInterface()) {
                n.setComment(new BlockComment(" this is a class "));
            }
            return super.visit(n, arg);
        }

        @Override
        public Node visit(MethodDeclaration n, Object arg) {
            n.setComment(new BlockComment(" this is a method "));
            return super.visit(n, arg);
        }
    }
}
假设以下输入文件
Test.java

import java.lang.System;

class Test {

    static class Foo {
    }

    static interface IFace {
    }

    public static void main(String... args) {
    }
}
输出将是

// enriched cource
import java.lang.System;

/* this is a class */
class Test {

    /* this is a class */
    static class Foo {
    }

    static interface IFace {
    }

    /* this is a method */
    public static void main(String... args) {
    }
}

您完全可以使用JavaParser来实现这一点。您可以访问树来查找所有类和方法(您可以使用访问者来执行此操作,也可以从文件的根(CompliationUnit)开始递归调用node.getChildrenNodes()。当您找到ClassOrInterfaceDeclaration或MethodDeclaration的实例时,只需使用方法node.setComment()添加注释

现在,您可以使用DumpVisitor转储文件(从AST返回到源代码),或者只需调用compliationUnit.toString()

免责声明:我是JavaParser的贡献者


Feed free可在此询问其他问题或在GitHub上打开一个问题。该项目目前维护在

如何确定追加位置。如果您有逻辑,则应阅读该行并重写该文件。通常,此类代码的制作是使用抽象语法树(AST)完成的现在还不清楚你在问什么。你在问Java中有哪些方法可以读取文本文件、解析文本文件并根据内容更新文件?目前还不清楚Java标记是因为你正在读取Java文件还是因为解决方案必须使用Java。请澄清。我使用过japa还是简单的文本阅读器你可以找到类名和它的行号,然后行号-1是我需要添加的行/*这是方法/类*/,这不是问题,我想在公共类abc之前添加一条注释,而不修改任何其他内容。这将通过使用正则表达式和缓冲区读取文本文件直到公共类XXX,然后重写原始内容来实现但这不是一种好的方法。如果找不到位置公共类XXX,您的代码以后可能无法编译。这可能是因为注释/*公共类XXX{….*/实际上我也认为这种方法,读取不同文件或字符串中的上层内容,在不同的文件/字符串中休息,在第一部分追加所需的数据,然后再追加代码的其余部分,但这不是一种好方法,加上它对小代码来说是可以的,如果代码有1000行或更多行,那该怎么办谢谢你的回答,我知道了我也试过了,但我不需要在屏幕上显示更改后的输出,而是保存在我从@shrikant.sharma处读到的精确文件中。看看我的更新答案。谢谢大家找到了解决方案,谢谢@SubOptimal和@Federico Tomassetti,实际上做了更改后很容易,fileWriter(cu.toString)完成任务了,@SubOptimal你能把你的工作项目和所需的jar shrikant一起寄给我吗。sharma606@gmail.com因为当我在eclipse中处理您的代码时,它给出了错误
Files.write(sourceFile.toPath(),cu.toString().getBytes(Charset.defaultCharset()),StandardOpenOption.TRUNCATE_EXISTING);)
,也希望了解您的做法。@shrikant.sharma您可能有打字错误
);)
,或者您使用的是7之前的Java版本。