FileWriter is not writing in to a file

后端 未结 5 998
谎友^
谎友^ 2021-01-15 09:04

I have below code

    try
    {
        FileWriter fileWriter = new FileWriter(\"C:\\\\temp\\\\test.txt\");
        fileWriter.write(\"Hi this is sasi This t         


        
5条回答
  •  囚心锁ツ
    2021-01-15 09:21

    Try this:

    import java.io.*;
    
    public class Hey
    
    {
        public static void main(String ar[])throws IOException
        {
    
                File file = new File("c://temp//Hello1.txt");
                // creates the file
                file.createNewFile();
                // creates a FileWriter Object
                FileWriter writer = new FileWriter(file); 
                // Writes the content to the file
                writer.write("This\n is\n an\n example\n"); 
                writer.flush();
                writer.close();
        }
    }
    

提交回复
热议问题