1
File filename =new File("D:/text.txt");

final Scanner scanner = new Scanner(filename);
while (scanner.hasNextLine()) {
    final String lineFromFile = scanner.nextLine();

    if(lineFromFile.contains("-----END CERTIFICATE REQUEST-----") ) { 
        FileWriter fw = new FileWriter(filename,true); //the true will append the new data

        fw.append(",");
        fw.write("wirte123");//appends the string to the file
        fw.close();

        System.out.println("I found "  +filename.getName());
        break;
     }
}

but for me it is adding after a new line....as shown below enter image description here

Need the output file as below enter image description here

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
String
  • 3,660
  • 10
  • 43
  • 66
  • 7
    Your file already contains that line break character. To get your desired result you'd have to remove that one first and then append your string – reto Jun 24 '16 at 08:12
  • probably the new line char is already in original file (edit: as he said) – Zeromus Jun 24 '16 at 08:13
  • Can you guys please suggest me ,how to break the new line char ..? – String Jun 24 '16 at 08:15
  • 1
    @String Get content of whole file and then check the last char and if '\n' or '\r\n' then replace to empty string. After that you can append your content. – Krzysiek Jun 24 '16 at 08:18
  • Take a look here: http://stackoverflow.com/questions/7454330/how-to-remove-newlines-from-beginning-and-end-of-a-string-java – Mistalis Jun 24 '16 at 08:32

2 Answers2

2

The original file already contains an ending new line. So if you append something to the original file, it will be written after that new line.

What is worse, is that a Scanner is a high level object that do not allow you to know the exact position in the file where an internal cursor could be positionned, because internally it reads large buffers (for a certificate file, the whole file is likely to be read at once).

So you should rather use a RandomAccessFile and its associated FileChannel to record the position of each begin of line, and to truncate the file just before a possible end of line.

Code could become:

    RandomAccessFile fw = new RandomAccessFile(filename, "rw");
    FileChannel fc = fw.getChannel();
    long oldpos;
    while (true) {
        oldpos = fc.position();
        final String lineFromFile = fw.readLine();

        if (lineFromFile.contains("-----END CERTIFICATE REQUEST-----")) {
            System.out.println("I found "  +filename.getName());*/
            fc.truncate(oldpos + lineFromFile.length());
            fw.writeBytes(",wirte123");//appends the string to the file
            break;
        }
    }
    fw.close();
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

You can use Bufferwriter with fileWriter and use append method of buffer writer and it will append to text instead of creating new line.

E.g.

 File filename = new File("D:/text.txt");
 FileWriter fileWritter = new FileWriter(filename,true);
 BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
 bufferWritter.append(",wirte123");
 bufferWritter.close();
 fileWritter.close(); 
Piyush Aghera
  • 965
  • 7
  • 13