我想用java代码在文件中的特定位置附加一些数据

我想用java代码在文件中的特定位置附加一些数据,java,file-io,append,read-write,Java,File Io,Append,Read Write,我想在java代码的文件中附加一些数据 我的文件包含以下数据: #JSGF V1.0; /** * JSGF Grammar for Hello World example */ grammar hello; public <greet> = (ANKIT)|(BHAVIK)|(MONIL)|(JAY)|(HIREN)|(KRUTIKA)|(RIKIN)|(YAGNESH)|(KRISHNA); 请给我一些适当的建议或示例代码 我这样做是为了达到以下目的 试一试{ 文件f

我想在java代码的文件中附加一些数据

我的文件包含以下数据:

#JSGF V1.0;

/**
 * JSGF Grammar for Hello World example
 */

grammar hello;

public <greet> = (ANKIT)|(BHAVIK)|(MONIL)|(JAY)|(HIREN)|(KRUTIKA)|(RIKIN)|(YAGNESH)|(KRISHNA);
请给我一些适当的建议或示例代码

我这样做是为了达到以下目的

试一试{ 文件f=新文件(“E:\new\u Workspace\Voice\u recognition\u modify\src\edu\cmu\sphinx\demo\helloworld\hello.gram”); RandomAccessFile raf=新的RandomAccessFile(f,“rw”)

但是我得到了一个文件,如下所示:
“A”然后是某个方形框图像“s”然后是某个方形框图像”等等

您可以使用文件读取器/文件写入器组合

阅读每一行,然后检查最后一个字符是否为;。如果是,则在此之前插入您的输入;我认为apache库有一个方法可以在另一个字符串中插入字符串。或者您可以自己编写一个方法

或者您可以使用RandomAccessFile并以rw模式打开文件。然后您可以修改现有文件。您可以使用调用readLine()并检查的相同过程; 使用RandomAccessFile的一个示例是

但有一个问题是——是否只有一行结尾

在粗略的未测试代码中

BufferedReader in = new BufferedReader(new FileReader("/tmp/fileIn"));
String str;
while ((str = in.readLine()) != null) {
    BufferedWriter out = new BufferedWriter(new FileWriter("/tmp/fileOut"));
    if (str.endsWith(";") { 
        //insert the variable before the ; here
    }
    out.write(str);
    out.close();
}
in.close();

我对RandmonAccessFile的记忆不太好,但上面的内容应该可以大致正常工作,尽管如果;始终是最后一个字符,RandomAccessFile会更干净。

我使用以下代码完成了这项操作。.获得成功

               File f = new File("E:\\NEw_Workspace\\Voice_recognition_modify\\src\\edu\\cmu\\sphinx\\demo\\helloworld\\hello.gram");
                    RandomAccessFile raf = new RandomAccessFile(f, "rw");

                    // Read a character
                   // char ch = raf.readChar();

                    // Seek to end of file
                    raf.seek((f.length())-1);

                    // Append to the end
                    raf.write(("|(KARAN);").getBytes());

                    raf.close();
                } catch (IOException e) {
                } 

似乎您想用您想要的文本替换第二个
之后的行。或者您想在最后一个
之前附加
(word)
。您能澄清一下吗?这不是一个明确的问题。请在stackoverflow伙计删除它之前描述更多内容!)我想附加|(word)第二次之后;每一次都应该在第二次之前追加;是的..应该有一行以结尾;…你能不能提供一些示例代码…?请参阅更新以回答。它未经测试,但应该会让你接近。我不想删除任何内容..它只是删除ma数据,我必须在第二次之前追加字符串“;“不是在第一次出现之前”;“那么有一个或两个;你的两个评论说的是不同的事情
BufferedReader in = new BufferedReader(new FileReader("/tmp/fileIn"));
String str;
while ((str = in.readLine()) != null) {
    BufferedWriter out = new BufferedWriter(new FileWriter("/tmp/fileOut"));
    if (str.endsWith(";") { 
        //insert the variable before the ; here
    }
    out.write(str);
    out.close();
}
in.close();
               File f = new File("E:\\NEw_Workspace\\Voice_recognition_modify\\src\\edu\\cmu\\sphinx\\demo\\helloworld\\hello.gram");
                    RandomAccessFile raf = new RandomAccessFile(f, "rw");

                    // Read a character
                   // char ch = raf.readChar();

                    // Seek to end of file
                    raf.seek((f.length())-1);

                    // Append to the end
                    raf.write(("|(KARAN);").getBytes());

                    raf.close();
                } catch (IOException e) {
                }