-1

I'm new to java and currently trying to write some strings into a text file using this tut : http://www.homeandlearn.co.uk/java/write_to_textfile.html

so here is my code :

public void savefile() throws IOException {      
    JOptionPane.showMessageDialog(null,"Hi,i'm in Try Block :|");
    FileWriter write = new FileWriter("asd.txt", true);
    PrintWriter print = new PrintWriter(write);
    JOptionPane.showMessageDialog(null, "File Opened");
    write.write("Knock Knock");
    print.flush();
    print.write("Hello ?");
    print.flush();
    print.printf("Hi?");
    print.flush();
    print.println("anybody there?");
    print.flush();
    JOptionPane.showMessageDialog(null, "Can you hear me ?");
    print.close();
    JOptionPane.showMessageDialog(null, "File Closed");         
}

and this is how I call the method:

try {
    savefile();
}
catch (IOException e) {
    JOptionPane.showMessageDialog(null, "Error: " + e.getMessage());
}

But nothing appears in the file! I'm really sick of this; what did I do wrong?

MC Emperor
  • 17,266
  • 13
  • 70
  • 106
Jafar Akhondali
  • 1,490
  • 1
  • 11
  • 23

5 Answers5

2

The code is fine.

You should be looking in the correct file. If you run the file in Eclipse or Netbeans, the created text file is located in your project directory.

MC Emperor
  • 17,266
  • 13
  • 70
  • 106
1
public void saveFile()
{
    BufferedWriter bufferedWriter = null;
    try
    {
        bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("asd.txt"))));
        bufferedWriter.writeLine("Hello world!");
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if(bufferedWriter != null)
        {
            try
            {
                bufferedWriter.close();
            }
            catch(IOException e) {}
        }
    }
}
EpicPandaForce
  • 71,034
  • 25
  • 221
  • 371
1

The code works perfectly. Test code:

public class ij3
{
    public void savefile() throws IOException 
    {      
        JOptionPane.showMessageDialog(null,"Hi,i'm in Try Block :|");
        FileWriter write = new FileWriter("asd.txt",true);
        PrintWriter print = new PrintWriter(write);
        JOptionPane.showMessageDialog(null,"File Opened");
        write.write("Knock Knock");
        print.flush();
        print.write("Hello ?");
        print.flush();
        print.printf("Hi?");
        print.flush();
        print.println("anybody there?");
        print.flush();
        JOptionPane.showMessageDialog(null,"Can you hear me ?");
        print.close();
        JOptionPane.showMessageDialog(null,"File Closed");         
    }
public static void main(String [] args)
{
    ij3 s = new ij3();      
    try
    {
            s.savefile();
        }
        catch (IOException e){
        JOptionPane.showMessageDialog(null,"Error: "+e.getMessage());
    }
    }
}
sonic_4vi
  • 31
  • 1
  • 5
0

In order to handle files,there are different kinds of Java classes like character streams and bytes stream. Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStream and OutputStream.All character stream classes are descended from Reader and Writer. There are different style to write to text file.So better to follow this link http://www.tutorialspoint.com/java/java_files_io.htm.Happy Coding

0

I tried the following code and its working fine. Please provide the exact error message u r getting

public class Writer {

/**
 * @param args
 */
public static void main(String[] args) {
    try {
        new Writer().savefile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public void savefile() throws IOException {      
    FileWriter write = new FileWriter("d:/asd.txt");
    PrintWriter print = new PrintWriter(write);
   write.write("Knock Knock");
    print.flush();
    print.write("Hello ?");
    print.flush();
    print.printf("Hi?");
    print.flush();
    print.println("anybody there?");
    print.flush();
    print.close();
  }

}