2011-01-20 94 views

回答

2
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"); 
       } 
     } 
} 
0

試一下這個(取自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"); 
    } 
} 
0
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(); 
} 
相關問題