Showing posts with label write to file. Show all posts
Showing posts with label write to file. Show all posts

Tuesday, May 8, 2012

JAVA Write to a file

// Write x to file
public void writeFile(String x, String file){
    try{
        // Create file
        FileWriter fstream = new FileWriter(file);
        // Create Writer
        BufferedWriter out = new BufferedWriter(fstream);
        // Write x to the file        
        out.write(x);
        //Close the output stream
        out.close();
    }catch (Exception e){
        //Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
}

Sunday, May 6, 2012

.NET C# Write to a file

public static bool WriteToFile(string fileName, string fileContent)
{
    fileName = fileName;
    System.IO.TextWriter tw;
    try
    {
        tw = new System.IO.StreamWriter(fileName, true);
        tw.WriteLine(fileContent);
        tw.Close();
        return true;
    }
    catch{}
    return false;
}