How do I save a String to a text file using Java?

后端 未结 24 1511
不知归路
不知归路 2020-11-22 04:18

In Java, I have text from a text field in a String variable called \"text\".

How can I save the contents of the \"text\" variable to a file?

24条回答
  •  深忆病人
    2020-11-22 04:42

    Just did something similar in my project. Use FileWriter will simplify part of your job. And here you can find nice tutorial.

    BufferedWriter writer = null;
    try
    {
        writer = new BufferedWriter( new FileWriter( yourfilename));
        writer.write( yourstring);
    
    }
    catch ( IOException e)
    {
    }
    finally
    {
        try
        {
            if ( writer != null)
            writer.close( );
        }
        catch ( IOException e)
        {
        }
    }
    

提交回复
热议问题