Tofiq
Tofiq

Reputation: 3001

How to create a file by extension(.ini) in java?

Hi I want create this file (boot.ini) and write the content to this file.How can create and write to this file?

Upvotes: 1

Views: 809

Answers (3)

Ilya Saunkin
Ilya Saunkin

Reputation: 19820

try {
    String content = "blah";
    BufferedWriter buf = new BufferedWriter(new FileWriter(new File("./boot.ini")));
    buf.write(content, 0, content.length());
    buf.close();        
}
catch(Exception e) {
    e.printStackTrace();
}

Upvotes: 0

David Weiser
David Weiser

Reputation: 5195

Try this (taken from this site:

import java.io.*;

public class WriteFile{

public static void main(String[] args) throws IOException{

  File f=new File("boot.ini");
  FileOutputStream fop=new FileOutputStream(f);

  if(f.exists()){
  String str="This data is written through the program";
      fop.write(str.getBytes());

      fop.flush();
      fop.close();
      System.out.println("The data has been written");
      }

      else
        System.out.println("This file is not exist");
  }
}

Upvotes: 0

Bnjmn
Bnjmn

Reputation: 2009

import java.io.*;

class FileOutputDemo 
{   

        public static void main(String args[])
        {              
                FileOutputStream out; // declare a file output object
                PrintStream p; // declare a print stream object

                try
                {
                        // Create a new file output stream
                        // connected to "boot.ini"
                        out = new FileOutputStream("boot.ini");

                        // Connect print stream to the output stream
                        p = new PrintStream( out );

                        p.println ("This is written to a file");

                        p.close();
                }
                catch (Exception e)
                {
                        System.err.println ("Error writing to file");
                }
        }
}

Upvotes: 2

Related Questions