-1

i just need your help. I'm learning java(OOP) and now days we are working on filing. But i got stuck on how to append data in the file. I have written the code and and here's the part of it which is showing the error. Can someone please help me what's wrong with it and why it is not working?

 package appending;
 import java.io.FileNotFoundException;
 import java.util.Formatter;
 import java.util.Scanner;
 import java.io.FileWriter;
 import java.io.BufferedWriter;


public class open {    
Formatter output;    
public void openFile() throws FileNotFoundException {    
    output = new Formatter("E:/thisFile.txt");
}
public void addData() {    
 Scanner input = new Scanner(System.in);
 data d = new data();
 System.out.println("Enter the data");
 d.setData(input.next(),input.nextInt());
 output.format("%s","Name and CMS:\t"+d.getData());
 FileWriter fileWritter = new FileWriter(File.getPath(),true);
 BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
 bufferWritter.write(d.getData());
 bufferWritter.close();
}
public void close() {
 output.close();
}
}
Kannan Thangadurai
  • 1,117
  • 2
  • 17
  • 36
jawaria
  • 1
  • 1

2 Answers2

0

Creating a text file (note that this will overwrite the file if it already exists):

PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
writer.println("The first line");
writer.println("The second line");
writer.close();

Creating a binary file (will also overwrite the file):

byte dataToWrite[] = //...
FileOutputStream out = new FileOutputStream("the-file-name");
out.write(dataToWrite);
out.close();

Answer gotten from: How do I create a file and write to it in Java?

If you want to use FileWriter. Try following code:

//FileWriter fw = new FileWriter(new File("path/to/test.txt"), true);
FileWriter fw = new FileWriter("path/to/test.txt", true);
fw.write("This is a sentence");
fw.close();

EDIT You should follow the conventions and capitalize your classes.

Community
  • 1
  • 1
  • Kannan Thangadurai FileWriter fileWritter = new FileWriter(File.getPath(),true); BufferedWriter bufferWritter = new BufferedWriter(fileWritter); bufferWritter.write(d.getData()); bufferWritter.close(); this part of code is giving error. – jawaria Nov 12 '15 at 10:25
  • I dont want to overwrite the data in the file, i want it with the existing data. – jawaria Nov 12 '15 at 10:29
  • Probably because File.getPath() is wrong (take a look at the javadoc). You should specify it as follows: new FileWriter("mytextfile.txt", true); new FileWriter(new File("mytextfile.txt"), true); –  Nov 12 '15 at 10:32
  • I tested it myself, the FileWriter code should append the data to the file and not overwrite it. –  Nov 12 '15 at 10:53
0

Can try,

public class FileAppend {

    public static void main(String[] args) {

        PrintWriter out = null;

        try{
            out = new PrintWriter(new BufferedWriter(new FileWriter("/home/rakesh/myfile.txt", true)));
            out.println("appended text");
        } catch(Exception e){
            e.printStackTrace();
        } finally{
            out.close();
        }

    }

}
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55