BhavikKama
BhavikKama

Reputation: 8790

I want To append Some data in the File at specific location With the java Code

I want to append some data in a file the java code.

My file contains the following data:

#JSGF V1.0;

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

grammar hello;

public <greet> = (ANKIT)|(BHAVIK)|(MONIL)|(JAY)|(HIREN)|(KRUTIKA)|(RIKIN)|(YAGNESH)|(KRISHNA);

and I want that whenever I append file that data come before the ; charachter

So if I add JAYA then it appends like following :

public <greet> = (ANKIT)|(BHAVIK)|(MONIL)|(JAY)|(HIREN)|(KRUTIKA)|(RIKIN)|(YAGNESH)|(KRISHNA)|(JAYA);

Please give me some proper suggession or sample code for this.

i had Done like this to achive this kind of stuff as follows..

try { 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.writeChars("|(ASHISH);");
                    raf.close();
                } catch (IOException e) {
                }

but i am getting an the out put in file like follows: "A "then some square box image" then s "then some square box image" and so on

Upvotes: 0

Views: 3256

Answers (2)

BhavikKama
BhavikKama

Reputation: 8790

I had Done that With the Following Code ..Got success.

               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) {
                } 

Upvotes: 0

RNJ
RNJ

Reputation: 15552

You could use a FileReader/FileWriter combination.

Read each line and then check if the last character is ;. If it is then insert your input before this ; I think the apache libraries have a method to insert strings inside another string. Or you could write a method yourself

Or you could use RandomAccessFile and open the file is rw mode. Then you could amend the existing file. You could use the same process of calling readLine() and checking for ; An example of using RandomAccessFile is here

One question though - is there only ever one line ending in ;

In rough untested code

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();

My memory is not too good on RandmonAccessFile but the above should roughly work although RandomAccessFile would be cleaner if the ; is always the last character

Upvotes: 1

Related Questions